|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 19:00:01 2025 UTC |
> 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>