DateTime->format(epoch) returning the wrong date

前端 未结 4 1965
春和景丽
春和景丽 2021-01-22 04:55

I am working on a project and I am having an issue formatting an epoch time to a human readable time.

I have the following epoch time 1428512160 and when I

相关标签:
4条回答
  • 2021-01-22 05:14

    Try the following code:

    $reportedTimeString = date("d-m-Y H:i:s", $supportDetails["Reported"]);

    Or the following:

    $date = new DateTime();
    $date->setTimestamp($supportDetails["Reported"]);
    $reportedTimeString = $date->format("d-m-Y H:i:s");
    
    0 讨论(0)
  • 2021-01-22 05:17

    The problem I see is with your formatting.

    If you look at PHP's date function you can see that you just need to write each portion of the desired date & time into a string.

    The following formatting gives the same output you were looking for:

    $dt = new DateTime($supportDetails["Reported"]); $reportedTimeString = $dt->format('d/m/Y H:i:s \G\M\TP T');

    0 讨论(0)
  • 2021-01-22 05:32

    You need to add an @ for the timestamp in the DateTime class, like this:

    $dt = new DateTime("@" . $supportDetails["Reported"]);
                      //^ See here
    

    You can also see this in the manual. And a quote from there:

    Unix Timestamp "@" "-"? [0-9]+ "@1215282385"

    Also note that the current timezone is getting ignored, which you can also see in the manual:

    Note: The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).

    0 讨论(0)
  • 2021-01-22 05:32

    Printing date and time is correct. Its based on what GMT you have set in your PHP. If you printing with GMT you will get required result.

    0 讨论(0)
提交回复
热议问题