Jump to content

Popping and Integer as a time then looping until you reach the end...


HarryMW

Recommended Posts

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.)

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

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.