# datetime - how to use epoch time in php by adding time offset value

113

I have this hex`2a ba ce 21` which I have converted to decimal`716885537`, now what I have asked to do is:
`2a ba ce 21` is given; now print last updated time in seconds (seconds that have elapsed since January 1 Midnight, 2000) - 4 Byte
For converting to standard epoch time, add time offset value 946684800 (time offset from Jan. 1970 to Jan. 2000)

I have searched alot and to be honest every thing got mixed... It would be great help if some one tell me how to do this in PHP.

365

#### Solution:

every thing got mixed...

Trying to unmix the thingsā¦
Presumably your hex bytes are in little-endian order, so you have to reverse before converting.

``````<?php
\$timestamp = hexdec(implode(array_reverse(explode(' ', '2a ba ce 21'))));
echo "seconds:  ", \$timestamp+946684800, "\n";
echo "readable: ", date('r', \$timestamp+946684800), "\n";
``````

what if i had to convert 2c 00 to last updated time in milliseconds as well that is of 2 byte.

The conversion goes just like above. If you want to join the milliseconds to the timestamp, add them divided by 1000 to it.

``````\$timestamp = hexdec(implode(array_reverse(explode(' ', '2a ba ce 21'))));
\$millisecs = hexdec(implode(array_reverse(explode(' ', '2c 00'))));
\$timestamp += 946684800 + \$millisecs/1000;
echo "seconds:  ", \$timestamp, "\n";
date_default_timezone_set('UTC');
echo "readable: ", date('Y-m-d H:i:s.', \$timestamp).substr("00\$millisecs", -3), "\n";
``````

People are also looking for solutions to the problem: php - Join two tables with group by condition

Source