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
Share on other sites

It's not 8601.

 

You can use substr for this, or a regular expression:

$string = 'P0DT0H4M14S';

preg_match('/P(\d+)DT(\d+)H(\d+)M(\d+)S/', $string, $foo );

echo "{$foo[2]} Hours, {$foo[3]} Minutes, {$foo[4]} seconds.";

-Dan

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.