How to convert a 13-digit Unix timestamp to a date and time?

I have a 13-digit time stamp 1443852054000, which I want to convert to a date and time, but it fails. I tried the following codes:

echo date('Ymd h:i:s',$item->timestamp); 

doesn't work for me as well this

 $unix_time = date('Ymdhis', strtotime($datetime )); 

and this:

 $item = strtotime($txn_row['appoint_date']); <?php echo date("Ymd H:i:s", $time); ?> 

what should i use?

+13
php timestamp unix-timestamp
source share
3 answers

This timestamp is indicated in milliseconds, not seconds. Divide it by 1000 and use the date function:

 echo date('Ymd h:i:s', $item->timestamp / 1000); // eg echo date('Ymd h:i:s',1443852054000/1000); // shows 2015-10-03 02:00:54 
+26
source share

You can achieve this with DateTime :: createFromFormat .

Since you have a timestamp with 13 digits , you need to divide it by 1000 to use it with DateTime , DateTime .:

 $ts = 1443852054000 / 1000; // we're basically removing the last 3 zeros $date = DateTime::createFromFormat("U", $ts)->format("Ymd h:i:s"); echo $date; //2015-10-03 06:00:54 

Demo

http://sandbox.onlinephpfunctions.com/code/d0d01718e0fc02574b401e798aaa201137658acb


You can set the default time zone to avoid any warnings.

 date_default_timezone_set('Europe/Lisbon'); 

Note

Read more about php date and time php right way

+1
source share

JavaScript uses a 13-digit timestamp to represent time in milliseconds. PHP 10 uses a digital timestamp to represent the time in seconds. So divide by 1000 and round to get 10 digits.

 $timestamp = 1443852054000; echo date('Ymd h:i:s', floor($timestamp / 1000)); 
0
source share

All Articles