Skip to main content
Published: March 26 2010, 7:33:00 PMUpdated: November 28 2020, 8:19:22 PM

I am using PHP. I want to display the time left for a listing in a nice ## days ## hours ## mins ##seconds format. How do I accomplish this?

 Summary
 

There are two methods. Parse the TimeLeft field or use the timestamp and EndTime with PHP DateTime class. To parse the TimeLeft field, please see the getPrettyTimeFromEbayTime function in the example on KB 1084. In this KB we will make use of the timestamp and EndTime to calculate the TimeLeft using php's DateTime and DateInterval classes. The API response is captured with simple xml's simplexml_load_file method:
$xmlResponse = simplexml_load_file($request);

The first step needed is to get the DateTime objects for the timestamp and EndTime:
$timestamp = new DateTime($xmlResponse->Timestamp);
$endtime = new DateTime($xmlResponse->Item->EndTime);

Now we need the DateInterval:
$diff = $endtime->diff($timestamp);

All that remains is to format the DateInterval:
$diff->format("%y years %m months...");

Attached is a fully self-contained PHP example using the Shopping API's GetSingleItem. It does build the format in such a way that year and years will appear correctly. Drop this file on your working LAMP or WAMP to run.

 


Version Info

The code example above was based on the versions specified below:

 API Schema Version  663

Additional Resources

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