php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74275 strtotime()
Submitted: 2017-03-20 03:24 UTC Modified: 2020-11-11 13:15 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: 252811115 at qq dot com Assigned: cmb (profile)
Status: Not a bug Package: Date/time related
PHP Version: 7.1.3 OS: window,linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
3 - 2 = ?
Subscribe to this entry?

 
 [2017-03-20 03:24 UTC] 252811115 at qq dot com
Description:
------------
if time is 00:00am to 00:59am 
or 00:00pm to 00:59pm 
strtotime()


Test script:
---------------
<?php
    $time1 = '5:20am';
    $time2 = '12:55pm';
    $time3 = '9:55pm';
    $time4 = '0:45pm';


    $date = date('Y-m-d');
    echo date('Y-m-d H:i:s',strtotime($date.$time1)).PHP_EOL;
    echo date('Y-m-d H:i:s',strtotime($date.$time2)).PHP_EOL;
    echo date('Y-m-d H:i:s',strtotime($date.$time3)).PHP_EOL;
    echo date('Y-m-d H:i:s',strtotime($date.$time4)).PHP_EOL;


?>



2017-03-20 05:20:00
2017-03-20 12:55:00
2017-03-20 21:55:00
1970-01-01 08:00:00

Expected result:
----------------
<?php
    $time1 = '5:20am';
    $time2 = '12:55pm';
    $time3 = '9:55pm';
    $time4 = '0:45pm';


    $date = date('Y-m-d');
    echo date('Y-m-d H:i:s',strtotime($date.$time1)).PHP_EOL;
    echo date('Y-m-d H:i:s',strtotime($date.$time2)).PHP_EOL;
    echo date('Y-m-d H:i:s',strtotime($date.$time3)).PHP_EOL;
    echo date('Y-m-d H:i:s',strtotime($date.$time4)).PHP_EOL;


?>

Actual result:
--------------
2017-03-20 05:20:00
2017-03-20 12:55:00
2017-03-20 21:55:00
1970-01-01 08:00:00

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-20 03:41 UTC] pajoye@php.net
-Package: Systems problem +Package: Date/time related
 [2017-03-21 13:46 UTC] danack@php.net
To me, 00:59pm doesn't look like a valid time.

Hours in a 12-hour format time have the range 1-12.
Hours in a 24-hour format time have the range 0-23.

Obviously the behaviour of strtotime here is unexpected, but you've passed it something that doesn't look like a valid timestamp, so there may not be a 'correct' behaviour for it to follow.
 [2020-11-11 13:15 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-11-11 13:15 UTC] cmb@php.net
> Hours in a 12-hour format time have the range 1-12.

That.  Using `new DateTimeImmutable()` also reveals the details:

    Failed to parse time string (2020-11-110:45pm) at position 14 (p): The timezone could not be found in the database

You should always check the return value of strtotime() (see also
example #2 on the doc page[1]).

[1] <https://www.php.net/strtotime>
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Nov 25 08:01:23 2020 UTC