HarryMW Posted March 23, 2011 Share Posted March 23, 2011 Hi... again. I really should donate to here or something, I use it so much and feel so bad. Ah well. All I need to do (in theory) is change something like this 1500 into a time which would obviously be 1PM. Basically I am going to be plotting a beautiful graph with all this but need the axis to begin with. In the case below the time it starts is 1500 and finishes at 2200. // LOOP FROM START TO END $i = $eventecho['etd']; $ii = $eventfecho['eta']; while ($i <= $ii) { if($i == $ii) break; echo $i . "<br>"; $i = $i + 30; } This outputs: 1500 1530 1560 1590 1620 1650 1680 1710 1740 1770 1800 1830 1860 1890 1920 1950 1980 2010 2040 2070 2100 2130 2160 2190 Which is should. But obviously I just want the values: 1500 1530 1600 1630 etc.. ? So how exactly do I get the raw value which would be: "1500" and "2200" to turn magically into a time. Thanks, Harry. (Again, apologies for always posting, but im in the shtook.) Quote Link to comment https://forums.phpfreaks.com/topic/231529-popping-and-integer-as-a-time-then-looping-until-you-reach-the-end/ Share on other sites More sharing options...
kenrbnsn Posted March 23, 2011 Share Posted March 23, 2011 You have to use time addition, not regular addition. Something like this: <?php $start = strtotime('3:00 PM'); $end = strtotime('10:00 PM'); $half_hour = 1800; // 1800 seconds in a 1/2 hour $tmp = array(); for($i = $start; $i <= $end; $i+=1800) { $tmp[] = date('g:i a',$i); } echo implode("<br>\n",$tmp); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/231529-popping-and-integer-as-a-time-then-looping-until-you-reach-the-end/#findComment-1191453 Share on other sites More sharing options...
HarryMW Posted March 24, 2011 Author Share Posted March 24, 2011 Thanks that works just perfectly. How would I then go to subtract the start time from the finish to get the duration and if say it was over an hour echo Hours otherwise Hour? Harry. Quote Link to comment https://forums.phpfreaks.com/topic/231529-popping-and-integer-as-a-time-then-looping-until-you-reach-the-end/#findComment-1191834 Share on other sites More sharing options...
kenrbnsn Posted March 24, 2011 Share Posted March 24, 2011 Try <?php $duration = $end - $start; echo ($duration > 3600)?'Hours':'Hour'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/231529-popping-and-integer-as-a-time-then-looping-until-you-reach-the-end/#findComment-1191846 Share on other sites More sharing options...
HarryMW Posted March 24, 2011 Author Share Posted March 24, 2011 That simply echo's "Hours". Harry. Quote Link to comment https://forums.phpfreaks.com/topic/231529-popping-and-integer-as-a-time-then-looping-until-you-reach-the-end/#findComment-1191865 Share on other sites More sharing options...
kenrbnsn Posted March 24, 2011 Share Posted March 24, 2011 Yes. I showed you a way to get the duration and echo Hour or Hours. That's what you asked. You didn't say what you wanted to do with it. Ken Quote Link to comment https://forums.phpfreaks.com/topic/231529-popping-and-integer-as-a-time-then-looping-until-you-reach-the-end/#findComment-1191885 Share on other sites More sharing options...
HarryMW Posted March 24, 2011 Author Share Posted March 24, 2011 subtract the start time from the finish to get the duration Well. Alright then. To make it clearer what I would like the code to do end product. It would take the $start + $end values. Minus the start from the end to get a value at which point it would put either Hours or Hour. Not just put the word Hours or Hour. But actually echo the time value of the event duration then add the Hours/Hour on the end. Harry. Quote Link to comment https://forums.phpfreaks.com/topic/231529-popping-and-integer-as-a-time-then-looping-until-you-reach-the-end/#findComment-1191962 Share on other sites More sharing options...
kenrbnsn Posted March 25, 2011 Share Posted March 25, 2011 Take a look at the function in this post. It's called time_duration. When used in a script, you pass it the duration in seconds and it returns a nicely formatted string: <?php echo time_duration('3645') . "<br>\n"; // prints 1 hour, 45 seconds echo time_duration('39547') . "<br>\n"; // prints 10 hours, 59 minutes, 7 seconds ?> See if you can use it in your script. Ken Quote Link to comment https://forums.phpfreaks.com/topic/231529-popping-and-integer-as-a-time-then-looping-until-you-reach-the-end/#findComment-1192046 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.