Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

time step 1

Status
Not open for further replies.

MJB3K

Programmer
Jul 16, 2004
524
GB
Hi, is there anyway of doing this:

Say i had a min time of 08:00 hours and an end time of 23:00 hours. How could i make it increase in time according to a variable value, say 20 mins.

08:00
08:20
08:40
09:00
II
VV
23:00
Code:
$min = "08:00";
$max = "23:00";
$step = 20;

Any ideas??


Regards,

Martin

Computing Help And Info:
 
I would do something like:

Code:
<?php

$start_time = '8:00';
$end_time = '23:00';
$step_time = '0:20';

$current = strtotime ($start_time);
$end = strtotime ($end_time);
$step = explode (':', $step_time);
$step = $step[0] * 3600 + $step[1] * 60;


print '<html><body><pre>';

while ($current <= $end)
{
	print date ('H:i', $current) . "\r\n";
	$current += $step;
}

print '</pre></body></html>';
?>

Note that the script will do nothing if $start is after $end (i.e., it won't wrap across days).

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
thanks :) a star for you.

just a quick question though:

Can you explain this bit of code to me?
Code:
$current = strtotime ($start_time);
$end = strtotime ($end_time);
$step = explode (':', $step_time);
$step = $step[0] * 3600 + $step[1] * 60;


Regards,

Martin

Computing Help And Info:
 
The first two lines convert the start and end times into unix timestamps. If you pass strtotime() something that looks like only a time, it assumes the current day.

The second two split the step (expressed as HH:MM) into its component hour-part and minute-part. Then it converts the step into seconds.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
In order to be able to use date() to output the times, I need a Unix timetick, which is a count of seconds. If the step is "01:15", that means "one hour, fifteen minutes".

Or 1 hour * 3600 seconds/hour + 15 minutes * 60seconds/minute.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top