This topic is locked

Covert Unix Epoch time from Rest API

2/18/2022 4:41:32 AM
PHPRunner Tips and Tricks
A
AJShumak author

Hi,

PHP Runner was great in getting the data using Rest API. But the DateTime fields are using Unix Epoch Numbers.

Any sugestions on how to get them to show as Date and time?

For example i get a returned number of 1645157548
but need it to show as Thursday, February 17, 2022 11:12:28 PM

admin 2/18/2022

Use 'View as' Custom and add the code that converts epoch time to something human readable.

Tons of examples on the web:
https://stackoverflow.com/questions/13477788/convert-epoch-time-to-date-php

A
AJShumak author 2/22/2022

Tried

$epoch = $value;
$dt = new DateTime("@$epoch");
echo $dt->format('Y-m-d H:i:s');

and other various suggested ways, No Luck... Any chance of some added assistance...anyone?

W
WilliamBDevClub member 2/23/2022

You can use view as custom as Sergey said and here is a sample:

if($value)
$value = date("F j\, Y", strtotime($value));
A
AJShumak author 2/23/2022

Hi,

Tried the code. All dates are showing as Jan 1st 1970...

W
WilliamBDevClub member 2/24/2022

Sorry, missed the part about epoch.

if($value)
date("Y-m-d H:i:s", substr($value, 0, 10));
A
AJShumak author 2/25/2022

Hi,

While i appreciate the effort...result is now back to original EPOCH number...

DealerModulesDevClub member 2/25/2022

Hi AJ,

Try this format. It may differ by an hour depending on how you have your daylight savings time configured on your server.

Based on the page that Sergey suggested, use your $value and substitute the "1645157548" and you should get your result.

echo date("l, F d, o H:i:s A", substr("1645157548", 0, 10));

Your code should look like:

$value = date("l, F d, o H:i:s A", substr($value, 0, 10));

Paul

A
AJShumak author 2/26/2022

Paul, Thank you! That is so much closer!

Tuesday, February 22, 2022 20:19:53 PM is showing. But is should actually show 22-Feb-2022 3:19 pm.
Friday, February 18, 2022 15:58:19 PM is showing. But it should be 18-Feb-2022 10:58 am

Any suggestion on how to correct the time? Not worried about the day of week. Just proper time.

DealerModulesDevClub member 2/28/2022

Hi AJ,

Right now your script is using the timezone from the hosting server location and not your location.
In the PHPRunner manual, take a look at the third section, this may help you. https://xlinesoft.com/phprunner/docs/output_directory_settings.htm
or...
Here is an good article on timezones from Fernando:
https://asprunner.com/forums/topic/28772-Guide-28--Place-the-server-in-a-time-zone

To format your date / time, I use the PHP manual online and it will show you the different format characters to use. This is located at https://www.php.net/manual/en/datetime.format.php

To drop the day of the week, just remove the lower case l in the beginning of the format so:
echo date("l, F d, o H:i:s A", substr("1645157548", 0, 10));
would look like
echo date("F d, o H:i:s A", substr("1645157548", 0, 10));

Paul