sblake161189 Posted June 13, 2011 Share Posted June 13, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239242-parse-time-to-something-normal/ Share on other sites More sharing options...
ManiacDan Posted June 13, 2011 Share Posted June 13, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239242-parse-time-to-something-normal/#findComment-1229136 Share on other sites More sharing options...
Psycho Posted June 13, 2011 Share Posted June 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239242-parse-time-to-something-normal/#findComment-1229203 Share on other sites More sharing options...
AbraCadaver Posted June 13, 2011 Share Posted June 13, 2011 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."; Quote Link to comment https://forums.phpfreaks.com/topic/239242-parse-time-to-something-normal/#findComment-1229238 Share on other sites More sharing options...
sblake161189 Posted June 15, 2011 Author Share Posted June 15, 2011 Thanks guys - it all works B-E-A-UTIFULLY! Quote Link to comment https://forums.phpfreaks.com/topic/239242-parse-time-to-something-normal/#findComment-1229929 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.