繁体   English   中英

将日期字符串转换为日期

[英]Converting date string to date

我正在尝试使用strtotimeJan 14th 2011格式的日期字符串转换为格式2011-01-14但它不起作用。

这种格式不适合strtotime ,如果是这样,我该如何转换它。 本月总是以3个字母的格式,如1月,2月,3月,4月,5月,6月,7月,8月,9月,10月,11月,12月

$date_str = "Jan 14th 2011";
$date = date_parse_from_format('M jS Y', $date_str);

echo $date->format('Y-m-d');

请参考这里: http//www.php.net/manual/en/datetime.formats.date.php

AFAIK,strtotime将英文文本日期或时间解析为Unix时间戳。

strtotime或类似的解决方案需要对每个用例进行一些更改。 如果你想更进一步并有一个永久的解决方案将原始字符串日期转换为时间格式你可以检查这个项目我做了https://github.com/raimonbosch/date.extraction我正在使用常规语法来检测各种日期格式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM