Muhammad Bilal
Muhammad Bilal

Reputation: 57

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

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.

Upvotes: 0

Views: 449

Answers (1)

Armali
Armali

Reputation: 19395

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";

Upvotes: 1

Related Questions