php的strtotime在处理am/pm时的一个BUG
今天在处理一个采集数据时发现采集到的时间是空的,到源网站看了一下,发现是有时间的。联想到之前一个am/pm的问题,然后就有了如下测试。
测试代码:
- $date = date('Y-m-d');
- $array = array('00:00am','00:00pm','00:01am','00:01pm', '01:01am','01:01pm','12:00am','12:00pm','12:01am','12:01pm');
- foreach ($array as $time) {
- echo $date . ' ' . $time.'<br />';
- echo strtotime($date . ' ' . $time).'<br />';
- echo date('Y-m-d H:i:s', strtotime($date . ' ' . $time)).'<br /><hr />';
- }
大家可以先自己想想预期的答案是什么,再往下看:
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这样的时间格式:
- $time = ('2011-04-11 00:01:00');
- $timestamp = strtotime($time);
- $time_format = date('Y-m-d H:ia', $timestamp);
- echo $time_format;
得到的结果是:2011-04-11 00:01am。
纠结吧!解决办法也很简单,判断小时如果是0或00,转为12即可。
建议继续学习:
扫一扫订阅我的微信号:IT技术博客大学习
- 作者:易水寒沙 来源: 易水小居
- 标签: strtotime
- 发布时间:2011-09-19 23:33:25
- [55] Oracle MTS模式下 进程地址与会话信
- [55] IOS安全–浅谈关于IOS加固的几种方法
- [54] 如何拿下简短的域名
- [53] 图书馆的世界纪录
- [53] android 开发入门
- [52] Go Reflect 性能
- [49] 【社会化设计】自我(self)部分――欢迎区
- [49] 读书笔记-壹百度:百度十年千倍的29条法则
- [38] 程序员技术练级攻略
- [32] 视觉调整-设计师 vs. 逻辑