gpt4 book ai didi

php - strtotime 返回 false

转载 作者:可可西里 更新时间:2023-11-01 12:43:50 28 4
gpt4 key购买 nike

我有这样的日期字符串:

"2012 28 Nov 21:00 CET"

strtotime("2012 年 11 月 28 日 21:00 CET") 返回 false。

有什么方法可以将这个字符串转换成日期对象,还是我需要以不同的方式格式化它?

最佳答案

2012 11 月 28 日 21:00 CET 是一种奇怪的日期/时间格式。 Y-D-M?你从哪里得到的?

无论如何,DateTime 对象有一个方法 createFromFormat 可以更好地解析:

$dt = DateTime::createFromFormat("Y d M H:i T", '2012 28 Nov 21:00 CET');
$ts = $dt->getTimestamp();
echo $ts; // 1354132800

在这里试试:http://codepad.viper-7.com/NfAmcw

strtotime 需要“英文文本日期时间”(according to the manual ), Y-D-M 不是。任何时候 strtotime 返回 false,它根本不理解您的字符串,这在该应用程序中是预期的。手册页上的注释处理了这个问题:

Note:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

同样,DateTime 是与日期或时间进行任何交互的高级工具。

文档

关于php - strtotime 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13614500/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com