[英]php make date with format mm-dd-yyyy
I have a date in the following format 我有以下格式的日期
MM-DD-YYYY
How can I convert this to UNIX time in PHP 如何在PHP中将其转换为UNIX时间
Thanks 谢谢
Having a small problem 有一个小问题
$date = strtotime($_POST['retDate']);
print $date; //Prints nothing
print $_POST['retDate']; //Prints 08-18-2009
If the format is always like that, I'd would try something like: 如果格式总是这样,我会尝试类似的方法:
list($m,$d,$y) = explode('-', '08-18-2009');
$time = mktime(0, 0, 0, $m, $d, $y);
print date('m-d-Y', $time);
As for your example, the problem is the function fails. 对于您的示例,问题在于该函数失败。 You should check it like so: 您应该像这样检查它:
if(($time=strtotime('08-18-2009'))!==false)
{
// valid time format
}
else
echo 'You entered an invalid time format';
or you can use php date obyek to do that, like this 或者您可以使用php date obyek这样做,
$tgl = "30-12-2013";
$tgl2 = DateTime::createFromFormat('d-m-Y', $tgl);
print_r($tgl2);
echo($tgl2->format('Y-m-d'));
i hope this can help... 我希望这可以帮助...
Use strtotime : 使用strtotime :
$str = "03-31-2009";
$unixtime = strtotime($str);
Since these answers and comments were provided, PHP has updated to include a native function that suits this situation perfectly. 自从提供了这些答案和评论以来,PHP已更新为包括一个完全适合这种情况的本机函数。 Check out PHP's DateTime object here . 在此处查看PHP的DateTime对象 。 This includes a createFromFormat
method that will do as requested. 这包括createFromFormat
要求执行的createFromFormat
方法。 In this particular example: 在此特定示例中:
$date = DateTime::createFromFormat('j-M-Y', $_POST['retDate']);
From there, you can format it as desired or perform any other date operations: 从那里,您可以根据需要设置其格式或执行任何其他日期操作:
echo $date->format('Y-m-d');
And that's it! 就是这样! Keep in mind that this is only provided in PHP 5.3.0
or higher! 请记住,这仅在PHP 5.3.0
或更高5.3.0
提供!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.