dwest Posted March 14, 2008 Share Posted March 14, 2008 Hi, I have a string representing time left on an auction which is returned to me from the eBay API. It reads like so: P2T9H37M8S I need to reformat this to read: 2 Days 9 Hours 37 Minutes When a designation hits zero I need it to disappear. As in: 1 Day 2 Hours 10 Minutes 23 Hours 3 Minutes 1 Hours 2 Minutes 58 Minutes And finally nothing at all appears. I'm totally ignorant on how to approach this as I'm not a scripting guru. Any help would be greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/ Share on other sites More sharing options...
soycharliente Posted March 14, 2008 Share Posted March 14, 2008 A way: <?php $time = "P2T9H37M8S"; // days $start = strpos($time, "P") + 1; $end = strpos($time, "T"); $days = substr($time, $start, $end-$start); // hours $start = strpos($time, "T") + 1; $end = strpos($time, "H"); $hours = substr($time, $start, $end-$start); // minutes $start = strpos($time, "H") + 1; $end = strpos($time, "M"); $minutes = substr($time, $start, $end-$start); // seconds $start = strpos($time, "M") + 1; $end = strpos($time, "S"); $seconds = substr($time, $start, $end-$start); echo ($days=="0" && $hours=="0" && $minutes=="0" && $seconds=="0") ? "<p>Time's up!</p>" : "<p>$days days $hours hours $minutes minutes $seconds seconds</p>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/#findComment-491949 Share on other sites More sharing options...
sasa Posted March 14, 2008 Share Posted March 14, 2008 try <?php $test = 'P2T0H1M8S'; $x =array(1 => 'Day', 'Hour', 'Minute'); preg_match('/P(\d+)T(\d+)H(\d+)M/',$test, $d); for ($i = 1; $i < 4; $i++){ $dare .= $d[$i] ? $d[$i].' '.$x[$i].($d[$i] > 1 ? 's ' : ' ') :''; } print_r($dare); ?> Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/#findComment-491971 Share on other sites More sharing options...
dwest Posted March 14, 2008 Author Share Posted March 14, 2008 Thanks so much for both replies! Both work but need tweaking due to my own oversight. The data returned by eBay represents time left on an auction. Thus it changes constantly. As such, when there are no more days it reads as: PT8H10M26S When there are no more hours it reads like: PTH10M26S etc., etc. until it would read: PTHMS How can this be accommodated? Thanks so much! Both are great solutions Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/#findComment-491980 Share on other sites More sharing options...
sasa Posted March 14, 2008 Share Posted March 14, 2008 <?php $test = 'PTH1M8S'; $x =array(1 => 'Day', 'Hour', 'Minute'); preg_match('/P(\d*)T(\d*)H(\d*)M/',$test, $d); for ($i = 1; $i < 4; $i++){ $dare .= $d[$i] ? $d[$i].' '.$x[$i].($d[$i] > 1 ? 's ' : ' ') :''; } print_r($dare); ?> Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/#findComment-491981 Share on other sites More sharing options...
dwest Posted March 14, 2008 Author Share Posted March 14, 2008 Thanks sasa! That seems to work well. Now I have to add in styling which I think I can do. I'll add to this post if I run into problems with that. You used regex correct? I haven't a clue how it works but would like to at least know the mechanism you're using. Can you post a brief explanation? Thanks again to you and to charlieholder Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/#findComment-491985 Share on other sites More sharing options...
dwest Posted March 14, 2008 Author Share Posted March 14, 2008 sasa, Regarding this line: $dare .= $d[$i] ? $d[$i].' '.$x[$i].($d[$i] > 1 ? 's ' : ' ') :''; I need to add divs and styles etc. One question: What is the significance of the *.* in *$dare .=* ? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/#findComment-491990 Share on other sites More sharing options...
Daniel0 Posted March 14, 2008 Share Posted March 14, 2008 What is the significance of the *.* in *$dare .=* ? The dot ( . ) is the concatenation operator, so if you put it before the assignment operator then the right operand will be added to the left operand (not added in the arithmetic sense, but by sticking things together). Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/#findComment-491991 Share on other sites More sharing options...
sasa Posted March 14, 2008 Share Posted March 14, 2008 '/P(\d*)T(\d*)H(\d*)M/' / start pattren P character 'P' ( start of 1st subpattern \d any digit * zero or more times (+ means one or more times) ) end of 1st subpattern T char 'T' ( start of 2nd subpattern \d any digit * zero or more times (+ means one or more times) ) end of 2nd subpattern etc. / end pattern Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/#findComment-491995 Share on other sites More sharing options...
dwest Posted March 14, 2008 Author Share Posted March 14, 2008 Thanks Daniel0! Please pardon my ignorance, but why is he doing that? Why not $dare= ? Thanks! BTW, I'm an artist so it's a miracle I can even remotely understand regex, but I'm getting there...slowly htp://www.smallartgems.com/collectors-gallery Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/#findComment-491997 Share on other sites More sharing options...
dwest Posted March 14, 2008 Author Share Posted March 14, 2008 Thanks sasa for the explanation of the mechanism. Much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/#findComment-491998 Share on other sites More sharing options...
sasa Posted March 14, 2008 Share Posted March 14, 2008 in array $d in index 1 to 3 is numbers of day, hour and minute (number of days is $d[1] etc.) i hope that you can print right output for that data Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/#findComment-492000 Share on other sites More sharing options...
Daniel0 Posted March 14, 2008 Share Posted March 14, 2008 Thanks Daniel0! Please pardon my ignorance, but why is he doing that? Why not $dare= ? Here is a more visual example. Perhaps that'll help with the understanding <?php $var1 = 'abc'; $var2 = 'def'; $var1 .= $var2; // $var1 now contains: abcdef // this is the same as: // $var1 = $var1 . $var2; //--- $var1 = 'abc'; $var2 = 'def'; $var1 = $var2; // $var1 now contains: def ?> You can do that with any operator, e.g. $num += 5 (add 5 to $num (the same as $num = $num + 5)). Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/#findComment-492003 Share on other sites More sharing options...
dwest Posted March 14, 2008 Author Share Posted March 14, 2008 Thanks sasa, The only thing I'm confused about is where to enclose the output in a div. Should I concatenate it in this string? $dare .= $d[$i] ? $d[$i].' '.$x[$i].($d[$i] > 1 ? 's ' : ' ') :''; If so, how? It does print properly BTW but I just need format it. Once I understand the concatenation I'll be fine. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/#findComment-492005 Share on other sites More sharing options...
dwest Posted March 14, 2008 Author Share Posted March 14, 2008 Thanks Daniel0! Clear on that now. Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/#findComment-492007 Share on other sites More sharing options...
dwest Posted March 14, 2008 Author Share Posted March 14, 2008 Update, Got the div formatting and styling to work. Wrapped $dare in it. echo '<div style="text-align:center;clear:both;background:#cccccc;padding:2px;"><em>Time Left: </em>' . $dare . ' '; Works like a charm! Many, many thanks to all who assisted. I got a good education on this one Quote Link to comment https://forums.phpfreaks.com/topic/96098-reformatting-a-time-string/#findComment-492016 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.