Skip to main content
Published: September 06 2007, 4:35:00 PMUpdated: November 28 2020, 3:56:31 PM

Time conversions from local time to GMT time are frequently required when using the eBay API. This discusses one simple way to handle such conversion in PHP.

Detailed Description

Many interactions with the eBay API require conversion from GMT to local time. In PHP, such conversion can be done in a number of ways. Later versions (5.x or higher) of PHP provide a DateTime object which makes such conversions simpler. However, DateTime related functions are included by default only in PHP versions equal and greater than 5.2.
In PHP 5.1.2 this functionality is marked to be experimental and has to be enabled at compile time.

There is also a PEAR Date package which provides robust time functionality including time zone conversion. However, this implies usage of PEAR which may be inappropriate at times.

Below is a simple method to convert between time zones. It simply uses the built-in PHP function gmstrftime function which is available in PHP 4.x and higher.

To use the ScheduleTime feature of the AddItem call, you need to indicate the scheduled start time for your item using GMT. If your user inputs schedule start time in local time, you need to convert to GMT.  This sample shows how to make such a conversion.



// Assume local schedule start time is 2008-09-05T08:05:00  (Pacific)
// Corresponding GMT start time is     2008-09-05T15:05:00 

setlocale(LC_TIME, 'en_US');
echo "Pacific Time   : ",   strftime("%Y-%m-%dT%H:%M:%S", mktime(8, 5, 0, 9, 5, 2008)) . "\n";
echo "Greenwich Time : ", gmstrftime("%Y-%m-%dT%H:%M:%S", mktime(8, 5, 0, 9, 5, 2008)) . "\n";


Sample Run

C:\>php TimeZone.php
Pacific Time   : 2008-09-05T08:05:00
Greenwich Time : 2008-09-05T15:05:00

How well did this answer your question?
Answers others found helpful