PHP - strtotime() 返回 1970

标签 php date time strtotime

以下代码片段:

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"));
  1. 日期和月份分隔符不应使用 .
  2. 您无法使用 - 分隔日期和时间。

如果您从其他来源获取输入,请尝试使用 str_replace:

echo date("d.m.Y-H:i:s", strtotime(str_replace(array(".", "-"), array("/", " "), "01.01.2000-11:12:32")));

输出:http://ideone.com/d19ATK

关于PHP - strtotime() 返回 1970,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41142170/

相关文章:

php - PDO和MySQL类型切换

date - JPA 标准 : Using order-by clause by truncating date

python - 如何解析没有日期的时间字符串和没有时间的日期字符串?

C++ 11 的 C++ 编译问题

php - Mysql插入,如果唯一键上有重复值,则插入不同的值

php - 哪些技术是在线实时聊天应用程序的最佳选择?

php - 有限制的结果总数

php - 将时间戳 DATE 重新转换回原始格式(编辑时)PHP MySQL

python-2.7 - 如何在Python中查找n天前的日期?

java - 使用 Java Reflection 调用方法时如何估计方法的执行时间