Search code examples
phpmongodbdateformatmongodate

how to format the mongoDate function properly


I am not sure if I'm doing this right, however the date function seems to display correctly, but for some reason it is returning the time in AM not PM, Example,

Right now, if I was to create a new date using:

$time = new mongoDate();

Then I save that on the data base, in the record it displays like so:

ISODate("2014-10-22T00:22:34.619Z")

Am assuming there's something I'm not doing correctly, but when I then format it, using:

date('d M, Y h:ia', $time->sec)

It display like so:

22 Oct, 2014 12:22am

I'm hoping this is something extremely obvious, but from what I can tell in the ISOdate, it's displaying in AM time, so my question is, why is it storing it in AM and not PM?


Solution

  • $time->sec has unix timestamp. When you use this timestamp in date() function, formated date will be based on your set timezone. That is why you are getting 12hour difference.

    $dt = new DateTime("@" . $time->sec); // DateTime instance in Z (zulu) timezone
    print_r($dt); // print the object
    $dt->setTimezone(new DateTimezone('... set your timezone ...'));
    print_r($dt); // print the object