以下代码片段:
echo date("d.m.Y-H:i:s", strtotime("01.01.2000-11:12:32"));
返回我:
01.01.1970-01:00:00
将“01.01.2000-11:12:32”转换为时间/日期对象以与当前时间戳进行比较的正确方法是什么?
例如
if (date("d.m.Y-H:i:s") > date("d.m.Y-H:i:s", strtotime("01.01.2000-11:12:32"))) {
echo "future";
} else {
echo "past";
}
最佳答案
这是由于本地化造成的。尝试提供不同的格式,因为格式很重要:
echo date("d.m.Y-H:i:s", strtotime("01/01/2000 11:12:32"));
echo date("d.m.Y-H:i:s", strtotime("01-01-2000 11:12:32"));
- 日期和月份分隔符不应使用
.
。 - 您无法使用
-
分隔日期和时间。
如果您从其他来源获取输入,请尝试使用 str_replace
:
echo date("d.m.Y-H:i:s", strtotime(str_replace(array(".", "-"), array("/", " "), "01.01.2000-11:12:32")));
关于PHP - strtotime() 返回 1970,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41142170/