IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

php的strtotime在处理am/pm时的一个BUG

易水小居 2011-09-19 23:33:25 累计浏览 3,297 次
本机暂存

今天在处理一个采集数据时发现采集到的时间是空的,到源网站看了一下,发现是有时间的。联想到之前一个am/pm的问题,然后就有了如下测试。
测试代码:

  1. $date = date('Y-m-d');
  2. $array = array('00:00am','00:00pm','00:01am','00:01pm', '01:01am','01:01pm','12:00am','12:00pm','12:01am','12:01pm');
  3. foreach ($arrayas$time){
  4.     echo $date . '' . $time.'<br />';
  5.     echo strtotime($date . '' . $time).'<br />';
  6.     echo date('Y-m-d H:i:s', strtotime($date . '' . $time)).'<br /><hr />';
  7. }

大家可以先自己想想预期的答案是什么,再往下看:

2011-04-11 00:00am

1970-01-01 07:00:00

――――――――――――――――――――――――――-
2011-04-11 00:00pm

1970-01-01 07:00:00

――――――――――――――――――――――――――-
2011-04-11 00:01am

1970-01-01 07:00:00

――――――――――――――――――――――――――-
2011-04-11 00:01pm

1970-01-01 07:00:00

――――――――――――――――――――――――――-
2011-04-11 01:01am
1302454860
2011-04-11 01:01:00

――――――――――――――――――――――――――-
2011-04-11 01:01pm
1302498060
2011-04-11 13:01:00

――――――――――――――――――――――――――-
2011-04-11 12:00am
1302451200
2011-04-11 00:00:00

――――――――――――――――――――――――――-
2011-04-11 12:00pm
1302494400
2011-04-11 12:00:00

――――――――――――――――――――――――――-
2011-04-11 12:01am
1302451260
2011-04-11 00:01:00

――――――――――――――――――――――――――-
2011-04-11 12:01pm
1302494460
2011-04-11 12:01:00

――――――――――――――――――――――――――-

怎么样,和你想像的有很大差别吧,为什么会这样呢?在PHP的帮助中有这样一段话:

More generally, the time of day may be given as ‘hour:minute:second’, where hour is a number between 0 and 23, minute is a number between 0 and 59, and second is a number between 0 and 59 possibly followed by ‘.’ or ‘,’ and a fraction containing one or more digits. Alternatively, ‘:second’ can be omitted, in which case it is taken to be zero. On the rare hosts that support leap seconds, second may be 60.

If the time is followed by ‘am’ or ‘pm’ (or ‘a.m.’ or ‘p.m.’), hour is restricted to run from 1 to 12, and ‘:minute’ may be omitted (taken to be zero). ‘am’ indicates the first half of the day, ‘pm’ indicates the second half of the day. In this notation, 12 is the predecessor of 1: midnight is ‘12am’ while noon is ‘12pm’. (This is the zero-oriented interpretation of ‘12am’ and ‘12pm’, as opposed to the old tradition derived from Latin which uses ‘12am’ for noon and ‘12pm’ for midnight.)

The time may alternatively be followed by a time zone correction, expressed as ‘shhmm’, where s is ‘+’ or ‘-’, hh is a number of zone hours and mm is a number of zone minutes. The zone minutes term, mm, may be omitted, in which case the one- or two-digit correction is interpreted as a number of hours. You can also separate hh from mm with a colon. When a time zone correction is given this way, it forces interpretation of the time relative to Coordinated Universal Time (utc), overriding any previous specification for the time zone or the local time zone. For example, ‘+0530’ and ‘+05:30’ both stand for the time zone 5.5 hours ahead of utc (e.g., India). This is the best way to specify a time zone correction by fractional parts of an hour. The maximum zone correction is 24 hours.

Either ‘am’/‘pm’ or a time zone correction may be specified, but not both.

这下我们清楚了,使用am/pm时,小时是限制为1-12的,使用0当小时数是错误的,但是如果你使用date来格式化输出时,却能得到00:01am这样的时间格式:

  1. $time = ('2011-04-11 00:01:00');
  2. $timestamp = strtotime($time);
  3. $time_format = date('Y-m-d H:ia', $timestamp);
  4. echo $time_format;

得到的结果是:2011-04-11 00:01am。
纠结吧!解决办法也很简单,判断小时如果是0或00,转为12即可。

同分类推荐文章

  1. 等了十年的 Go 链式管道,终于来了:seq 让你像写 Scala 一样写 Go (2026-06-25 18:38:18)
  2. Go 实验特性详解 (2026-06-21 10:05:27)
  3. amd64 微架构级别对 Go 程序性能提升多少? (2026-06-21 09:38:49)

查看更多 后端 文章 →

建议继续学习

  1. 使用gettext来支持PHP的多语言 (累计阅读 39,268)
  2. WordPress插件开发 -- 在插件使用数据库存储数据 (累计阅读 29,164)
  3. Paypal接口详细代码(PHP版,非API接口) (累计阅读 19,408)
  4. 我的PHP,Python和Ruby之路 (累计阅读 13,146)
  5. include(“./file.php”)和include(“file.php”)区别 (累计阅读 12,789)
  6. 15个最好的免费开源电子商务平台 (累计阅读 12,541)
  7. Redis消息队列的若干实现方式 (累计阅读 12,088)
  8. 到底什么是MVC? (累计阅读 11,865)
  9. 整理了一份招PHP高级工程师的面试题 (累计阅读 11,708)
  10. Rolling cURL: PHP并发最佳实践 (累计阅读 11,488)