Req #71802 [Com]: strtotime parses "13:01 PM" as 16:01
| From: | php at richardneill dot org | Date: | Sat, 12 Mar 2016 17:25:29 +0000 |
| Subject: | Req #71802 [Com]: strtotime parses "13:01 PM" as 16:01 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-199781@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=71802&edit=1
ID: 71802
Comment by: php at richardneill dot org
Reported by: php at richardneill dot org
Summary: strtotime parses "13:01 PM" as 16:01
Status: Open
Type: Feature/Change Request
Package: Date/time related
Operating System: Linux
PHP Version: 5.6.19
Block user comment: N
Private report: N
New Comment:
Thanks - I had no idea that there even was such a thing as a military P timezone.
My workaround, which may help anyone else, is just to do:
str_replace (array ("a.m.","p.m."), array("am","pm"),
strtolower($input));
before parsing with strtotime().
That way, "13:01 P.M." or "14:01 A.M." will always fail, rather than be
valid-but-wrong.
Correct use of am or pm is still fine.
Previous Comments:
------------------------------------------------------------------------
[2016-03-12 04:52:37] requinix@php.net
https://3v4l.org/2beus
Since "13:01 PM" isn't valid as time with meridian, the other behavior is to parse
the P as a timezone (military style where P=UTC-3). The M is a timezone too but ignored.
As a request, though, this doesn't sound unreasonable: allow an AM/PM after any time
specification, and warn or ignore if H>12.
For a workaround you could use date_parse() then check if hour>12 and tz_addr={A,P}. It'd be
work to construct a timestamp on your own from there, so maybe if you detect this problem you could
try a regex to remove /[AaPp]\.?[Mm]\.?/ from the input.
------------------------------------------------------------------------
[2016-03-11 17:15:00] php at richardneill dot org
Description:
------------
The time expression "13:01 P.M." is technically malformed (it's tautologous), but
people are still likely to use it sometimes. PHP mis-converts it, turning 13 into 16 !.
(In comparison, the Unix date command rejects it)
Test script:
---------------
PHP:
<?
echo date ("Y-m-d H:i:s", strtotime("13:01 P.M."));
#result: 2016-03-11 16:01:00
#expected: 2016-03-11 13:01:00
echo date ("Y-m-d H:i:s", strtotime("12:01 P.M."));
#result: 2016-03-11 12:01:00
#correct
?>
Shell:
$ date -d '13:01 P.M.';
date: invalid date â13:01 P.M.â
$ date -d '12:01 P.M.';
Fri Mar 11 12:01:00 GMT 2016
Expected result:
----------------
PHP shouldn't be off-by-3.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=71802&edit=1