Reputation: 5398
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?
Upvotes: 0
Views: 544
Reputation: 71
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
Upvotes: 2
Reputation: 538
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
Upvotes: 0