Is there a PHP function(or combined functions) which can directly convert <time> in GPX to local time? [duplicate]

StackOverflow https://stackoverflow.com/questions/20768940

Question

I am using simplexml_load_file to parse contains of a trip log file (.GPX). In it, the tag <time> records are of the GMT, and I am looking for a PHP funciton or a simple combination of PHP functions to convert it to my local time (GMT+8). I have looked through here and still can not figure out what to do.

To be specific, is there any PHP function which takes in a string like "2013-11-22T04:14:30Z" and returns "2013-11-22 12:14:30" (I wish to display datetime in this format)? I know it can be achieved by writing a function by myself, but I think there could be a standard PHP function that does this.

Was it helpful?

Solution

Pretty easy using strtotime & date:

$time = '2013-11-22T04:14:30';
$time_z = '2013-11-22T04:14:30Z';

$add_time = ' + 8 hours';

$unixtime = strtotime($time);
$unixtime_z = strtotime($time_z);

echo 'Unixtime: ' . date('Y-m-d h:i:s', $unixtime) . '<br />';
echo 'Unixtime Z: ' . date('Y-m-d h:i:s', $unixtime_z) . '<br />';

echo 'Unixtime (+ 8 hours): ' . date('Y-m-d h:i:s', $unixtime . $add_time) . '<br />';
echo 'Unixtime Z (+ 8 hours): ' . date('Y-m-d h:i:s', $unixtime_z . $add_time) . '<br />';

The output would be:

Unixtime: 2013-11-22 04:14:30

Unixtime Z: 2013-11-21 11:14:30

Unixtime (+ 8 hours): 2013-11-22 04:14:30

Unixtime Z (+ 8 hours): 2013-11-21 11:14:30

The key for this to work is the $add_time which is just me adding + 8 hours to the date string. But there are other methods to handle this based on your larger needs & input data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top