在 PHP 中将字符串转换为日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2222851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert String To date in PHP
提问by streetparade
How can I convert this string 05/Feb/2010:14:00:01to unixtime ?
如何将此字符串转换05/Feb/2010:14:00:01为 unixtime ?
回答by Sarfraz
Use the strtotimefunction:
使用strtotime函数:
Example:
例子:
$date = "25 december 2009";
$my_date = date('m/d/y', strtotime($date));
echo $my_date;
回答by erisco
For PHP 5.3 this should work. You may need to fiddle with passing $dateInfo['is_dst'], wasn't working for me anyhow.
对于 PHP 5.3,这应该可以工作。您可能需要摆弄传递 $dateInfo['is_dst'],无论如何对我不起作用。
$date = '05/Feb/2010:14:00:01';
$dateInfo = date_parse_from_format('d/M/Y:H:i:s', $date);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
$dateInfo['month'], $dateInfo['day'], $dateInfo['year'],
$dateInfo['is_dst']
);
Versions prior, this should work.
之前的版本,这应该有效。
$date = '05/Feb/2010:14:00:01';
$format = '@^(?P<day>\d{2})/(?P<month>[A-Z][a-z]{2})/(?P<year>\d{4}):(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})$@';
preg_match($format, $date, $dateInfo);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
date('n', strtotime($dateInfo['month'])), $dateInfo['day'], $dateInfo['year'],
date('I')
);
You may not like regular expressions. You could annotate it, of course, but not everyone likes that either. So, this is an alternative.
你可能不喜欢正则表达式。当然,您可以对其进行注释,但也不是每个人都喜欢这样。所以,这是一个替代方案。
$day = $date[0].$date[1];
$month = date('n', strtotime($date[3].$date[4].$date[5]));
$year = $date[7].$date[8].$date[9].$date[10];
$hour = $date[12].$date[13];
$minute = $date[15].$date[16];
$second = $date[18].$date[19];
Or substr, or explode, whatever you wish to parse that string.
或 substr 或爆炸,无论您想解析该字符串。
回答by Johannes Gorset
You should look into the strtotime() function.
您应该查看strtotime() 函数。
回答by Vladislav Rastrusny
回答by Dashrath
$d="05/Feb/2010:14:00:01";
$dr= date_create_from_format('d/M/Y:H:i:s', $d);
echo $dr->format('Y-m-d H:i:s');
here you get date string, give format specifier in ->format() according to format needed
在这里你得到日期字符串,根据需要的格式在 ->format() 中给出格式说明符
回答by dev-null-dweller
Simple exploding should do the trick:
简单的爆炸应该可以解决问题:
$monthNamesToInt = array('Jan'=>1,'Feb'=>2, 'Mar'=>3 /*, [...]*/ );
$datetime = '05/Feb/2010:14:00:01';
list($date,$hour,$minute,$second) = explode(':',$datetime);
list($day,$month,$year) = explode('/',$date);
$unixtime = mktime((int)$hour, (int)$minute, (int)$second, $monthNamesToInt[$month], (int)$day, (int)$year);
回答by enor
Try this:
尝试这个:
$new_date=date('d-m-Y', strtotime($date));
回答by Rick
If it's a string that you trust meaning that you have checked it before hand then the following would also work.
如果它是您信任的字符串,这意味着您事先已经检查过它,那么以下方法也适用。
$date = new DateTime('2015-03-27');

