Asked  1 Year ago    Answers:  5   Viewed   9 times

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 put this through epochconverter.com I get the human time of 08/04/2015 17:56:00 GMT+1:00 DST as expected.

I then use the following code in order to perform the conversion from the epoch time to a human date time.

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

$supportDetails[Reported] is the epoch time (I've printed it so I know it's correct).

The result I get back however is 08-04-2160 14:28:51.

 Answers

1

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).

Thursday, April 1, 2021
3

DateInterval is buggy on windows platform. See bug #51183. The official answer seems to be "use VC9 builds instead for now".

Thursday, April 1, 2021
 
relipse
 
3

I use following wrapper class in my php5.2 apps: http://pastebin.ca/2051944. Untill php5.3 was released - it saves much my time

Saturday, May 29, 2021
 
TuomasR
 
2

The reason for this behaviour is that when you don't specify the missing parts of a date/time input to DateTime::createFromFormat, it uses the values from the current local date and time. In Auckland, that is October 31st and so it tries to make a date out of September 31 2019, which comes out as October 1 2019. To avoid this problem, use a ! at the start of the format string; this will instead substitute values from January 1 1970, 00:00:00 (the Unix Epoch) as required for those that are not specified in the time value:

echo DateTime::createFromFormat('!F-Y', 'September-2019')
    ->setTimeZone(new DateTimeZone('Pacific/Auckland'))
    ->format('Y-m-d');

Output:

2019-09-01

Demo on 3v4l.org

Tuesday, August 31, 2021
 
Keith
 
3

Based on the documentation (and brief testing), moment.js creates wrappers around date objects. The statement:

var now = moment();

creates a "moment" object that at its heart has a new Date object created as if by new Date(), so hours, minutes and seconds will be set to the current time.

The statement:

var releaseDate = moment("2012-09-25");

creates a moment object that at its heart has a new Date object created as if by new Date(2012, 8, 25) where the hours, minutes and seconds will all be set to zero for the local time zone.

moment.diff returns a value based on a the rounded difference in ms between the two dates. To see the full value, pass true as the third parameter:

 now.diff(releaseDate, 'days', true)
 ------------------------------^

So it will depend on the time of day when the code is run and the local time zone whether now.diff(releaseDate, 'days') is zero or one, even when run on the same local date.

If you want to compare just dates, then use:

var now = moment().startOf('day'); 

which will set the time to 00:00:00 in the local time zone.

Tuesday, September 14, 2021
 
Adam
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :