技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> PHP --> php的strtotime在处理am/pm时的一个BUG

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

浏览:2490次  出处信息

今天在处理一个采集数据时发现采集到的时间是空的,到源网站看了一下,发现是有时间的。联想到之前一个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 ($array as $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. 五四陈透过PHP看JAVA系列:strtotime    (阅读:2251)
  2. 令人困惑的strtotime    (阅读:849)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1