简体   繁体   中英

convert to 24 hour format then timestamp

My datetime formatting is not working. I need to convert a 12 hour format time to 24 hour format after that the time's timestamp is required. so my code is

$to =  DateTime::createFromFormat('h:i A','1:00 AM');
echo $to->format('d M y H:i').'<br/>'; 
echo $to->getTimestamp();  

My 2nd line code's desired result is 24 hour format of 28 Sep 15 1:00 AM and then timestamp of this datetime. Whats wrong in my code?

My Problem is the timestamp of 28 Sep 15 1:00 AM is smaller than the timestamp of 28 Sep 15 8:00 PM . But WHY?

https://en.wikipedia.org/wiki/Unix_time

... defined as the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970

--

My Problem is the timestamp of 28 Sep 15 1:00 AM is smaller than the timestamp of 28 Sep 15 8:00 PM. But WHY?

Being timestamp "the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970" obviously 28 Sep 15 1:00 AM will always be smaller than 28 Sep 15 8:00 PM the same way that 28 Sep 15 00:00 AM will always be smaller than 28 Sep 15 00:00 PM

Add this line after the first one:

$to->modify('+12 hours');

EDIT: I'm not really getting what you need exactly, so here you have some samples:

$am = DateTime::createFromFormat('h:i A','1:00 AM');
echo $am->format('d M y H:i');   // prints '28 Sep 15 01:00'
echo $am->format('d M y h:i A'); // prints '28 Sep 15 01:00 AM'

$pm = DateTime::createFromFormat('h:i A','8:00 PM');
echo $pm->format('d M y H:i');   // prints '28 Sep 15 20:00'
echo $pm->format('d M y h:i A'); // prints '28 Sep 15 08:00 PM'

Code sample here: https://3v4l.org/PvAGm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM