I am getting an input in milliseconds. E.G: $timestamp = 1479844722190;
I convert that timestamp using $date = date('Y-m-d h:i:s', $timestamp / 1000);
I convert back to a timestamp using strtotime($date);
My result is 1479801522
The same thing happens when using gmdate()
.
The actual code:
date():
$timestamp = get_timestamp(); // 1479847575022
$date = date('Y-m-d h:i:s', $timestamp / 1000); // 2016-11-22 08:46:15
strtotime($date); // 1479804375
gmdate():
$timestamp = get_timestamp(); // 1479847661576
$date = gmdate('Y-m-d h:i:s', $timestamp / 1000); // 2016-11-22 08:47:41
strtotime($date); // 1479804461
Could someone tell my why this happens, and whether there is a way to do this without creating a time-machine like this?
UPDATE:
Putting date_default_timezone_set('UTC')
beforehand gives the following results:
timestamp: 1479848532028
date: 2016-11-22 09:02:12
reverted: 1479805332
timestamp: 1479848622222
date: 2016-11-22 09:03:42
reverted: 1479805422
After a sleeping on it, I found the answer.
gmdate('Y-m-d h:i:s', $timestamp / 1000);
should really be:
gmdate('Y-m-d H:i:s', $timestamp / 1000);