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

113

I have this hex2a ba ce 21 which I have converted to decimal716885537, 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

Answer

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

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.