Jump to content

Parse Time to something normal


sblake161189

Recommended Posts

Hi All,

 

I am using an eBay API to display some results in a table on a PHP page.

 

I am currently using the following code to display 'Time Left' until the auction ends.

 

<?php echo $item->sellingStatus->timeLeft ?>

 

And this outputs something similar to "P0DT0H4M14S" ie. 0 Days, 0 Hours, 4 Mins, 14 Seconds.

 

Is there any easy way to make this look more normal like

 

4m, 4s - using substr() doesnt work becuase it depends on whether the values are single or double figure values...

 

Any ideas im stuck... ive read somewhere its ISO-8601...

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/239242-parse-time-to-something-normal/
Share on other sites

Since the string appears to always contain values for each time period (even if it is 0) you could also you a simpler regex expression:

preg_match_all("#\d+#", $string, $foo);
list($days, $hours, $minutes, $seconds) = $foo[0];
echo "{$days} Days, {$hours} Hours, {$minutes} Minutes, {$seconds} seconds.";

 

Not sure if that is more efficient or not, just providing an alternative solution as it helps to think differently when trying to solve problems.

Or different again:

 

// you can also have arg references instead of returning or return an array
list($d, $h, $m, $s) = sscanf('P0DT0H4M14S', 'P%dDT%dH%dM%dS');
echo "{$d} Days, {$h} Hours, {$m} Minutes, {$s} seconds.";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.