PHP strtotime 转换不适用于某些日期时间

标签 php datetime strtotime

我正在尝试对 Web 服务结果集中的日期时间条目(从不为空)执行 strtotime。

在循环和转换时,我发现很多转换都无法使用 strtotime。问题是什么?有效和无效的格式之间没有区别。

1431109502 - 08-05-2015 13:25:02
- 06-15-2015 12:05:32 -- Didn't work (any that start with - didn't convert)
- 06-25-2015 11:30:30
- 06-15-2015 10:58:56
- 06-27-2015 20:33:31
- 06-28-2015 00:13:37
1431054141 - 07-05-2015 22:02:21
- 07-17-2015 07:23:57
- 07-22-2015 02:30:24
1431090998 - 08-05-2015 08:16:38
1433774646 - 08-06-2015 09:44:06

编辑:这是我用来尝试获得 100% 转化的一些代码。

$t = date_create_from_Format( 'd-m-Y H:i:s', trim($arrCurrentAlarms[$i]["DateTime"]) );
$sqldt = $t->format('Y-m-d H:i:s');

给我的输入总是采用这种格式:06-25-2015 11:30:30 (月日年时分秒)

最佳答案

那是因为 strtotime 猜错了你的日期:

date('r', strtotime('08-05-2015 13:25:02')) -> Fri, 08 May 2015 13:25:02 -0500
date('r', strtotime('06-25-2015 11:30:30')) -> Wed, 31 Dec 1969 18:00:00 -0600

请注意,现在第一个值出现在 5 月 8 日:strtotime 将您的日期视为 dd-mm-yyyy,而您确实有 mm-dd-yyyy .

请注意第二个日期是 1969 年 12 月 - 这就是整数 0(又名 bool 值 FALSE)作为我特定时区中的日期出现的方式。 FAILURE 为 FALSE,因为日历中没有月份 25

因为它猜错了,你必须改为执行 date_create_from_format(),并告诉 php 你的日期格式。

关于PHP strtotime 转换不适用于某些日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31865489/

相关文章:

sql - 如何检查 SQL Server 日期时间列是否为空?

mysql - 选择范围内的MySQL日期时间

php - strtotime 到特定日期的时间问题

php - 如何从 PHP 中的日期时间减去 1 天 16 小时

php - Symfony2 : determine if a controller is called from a development environment or from a production environment

php - 我可以使用 SET 选项将 ON DUPLICATE KEY UPDATE 与 INSERT 查询一起使用吗?

php - 无法在 Windows 10 Pro build 1809 : SSL operation failed with code 1 上安装 Composer

c++ - 在 C++ 中使用 mktime 的纪元

php - 当前日期 + 2 个月

javascript - 使用 php 从网络服务器读取时,JSON 文件返回 null。