PDA

View Full Version : create time range by minutes, secs, or hours



fci
05-26-2006, 09:39 PM
I posted in response to a thread but it is fairly generic and may be of use to more than a few people.

<?php

/**
* create_time_range
*
* @param mixed $start start time, e.g., 9:30am or 9:30
* @param mixed $end end time, e.g., 5:30pm or 17:30
* @param string $by 1 hour, 1 mins, 1 secs, etc.
* @access public
* @return void
*/
function create_time_range($start, $end, $by='30 mins') {

$start_time = strtotime($start);
$end_time = strtotime($end);

$current = time();
$add_time = strtotime('+'.$by, $current);
$diff = $add_time-$current;

$times = array();
while ($start_time < $end_time) {
$times[] = $start_time;
$start_time += $diff;
}
$times[] = $start_time;
return $times;
}

// create array of time ranges
$times = create_time_range('9:30', '17:30', '30 mins');

// more examples
// $times = create_time_range('9:30am', '5:30pm', '30 mins');
// $times = create_time_range('9:30am', '5:30pm', '1 mins');
// $times = create_time_range('9:30am', '5:30pm', '30 secs');
// and so on

// format the unix timestamps
foreach ($times as $key => $time) {
$times[$key] = date('g:i:s', $time);
}


print '<pre>'. print_r($times, true).'</pre>';
/*
* result
*
Array
(
[0] => 9:30:00
[1] => 10:00:00
[2] => 10:30:00
[3] => 11:00:00
[4] => 11:30:00
[5] => 12:00:00
[6] => 12:30:00
[7] => 1:00:00
[8] => 1:30:00
[9] => 2:00:00
[10] => 2:30:00
[11] => 3:00:00
[12] => 3:30:00
[13] => 4:00:00
[14] => 4:30:00
[15] => 5:00:00
[16] => 5:30:00
)

*/

?>

Jester1981
05-15-2009, 02:38 PM
Hey there,

I found this function from ya a while ago - but I recently needed it to return a 24 hour timestamp, rather going from 12:00:00 to 1:00:00 - my script treated this as 1am, but it gave me some inspriation, and I wrote this quickly:



function create_time_range2($start, $end, $by) {
// Create UNIX Timestamps
$start_exp = explode(":",$start);
$end_exp = explode(":",$end);
$start_time = mktime($start_exp[0],$start_exp[1],$start_exp[2],date(m),date(d),date(Y));
$end_time = mktime($end_exp[0],$end_exp[1],$end_exp[2],date(m),date(d),date(Y));

setlocale(LC_TIME,"english-aus");

// Create the array
$times = array();
for ( ;$start_time < $end_time; ) {
$times[] = date('H:i:s',$start_time);
$temp = ($start_time + (60 * $by));
$start_time = $temp;
}
$times[] = date('H:i:s',$start_time);
return $times;
}


Hope it helps ;)
Jesse.

petra5
06-16-2009, 08:01 PM
I came across the below code, but I don't know how to display the results in a drop down menu, where user selects one of the option. Can somebody please help as I am quite new with PHP?
At the end 'print' is used to show the results, but how would I write the code as a part of select tag? something like:

<option value=
"<?php
echo "$times[$key]";
?>"
>

</option>

Does somebody know the answer to this as I really don't know?





I posted in response to a thread but it is fairly generic and may be of use to more than a few people.

<?php

/**
* create_time_range
*
* @param mixed $start start time, e.g., 9:30am or 9:30
* @param mixed $end end time, e.g., 5:30pm or 17:30
* @param string $by 1 hour, 1 mins, 1 secs, etc.
* @access public
* @return void
*/
function create_time_range($start, $end, $by='30 mins') {

$start_time = strtotime($start);
$end_time = strtotime($end);

$current = time();
$add_time = strtotime('+'.$by, $current);
$diff = $add_time-$current;

$times = array();
while ($start_time < $end_time) {
$times[] = $start_time;
$start_time += $diff;
}
$times[] = $start_time;
return $times;
}

// create array of time ranges
$times = create_time_range('9:30', '17:30', '30 mins');

// more examples
// $times = create_time_range('9:30am', '5:30pm', '30 mins');
// $times = create_time_range('9:30am', '5:30pm', '1 mins');
// $times = create_time_range('9:30am', '5:30pm', '30 secs');
// and so on

// format the unix timestamps
foreach ($times as $key => $time) {
$times[$key] = date('g:i:s', $time);
}


print '<pre>'. print_r($times, true).'</pre>';
/*
* result
*
Array
(
[0] => 9:30:00
[1] => 10:00:00
[2] => 10:30:00
[3] => 11:00:00
[4] => 11:30:00
[5] => 12:00:00
[6] => 12:30:00
[7] => 1:00:00
[8] => 1:30:00
[9] => 2:00:00
[10] => 2:30:00
[11] => 3:00:00
[12] => 3:30:00
[13] => 4:00:00
[14] => 4:30:00
[15] => 5:00:00
[16] => 5:30:00
)

*/

?>

Inigoesdr
06-20-2009, 05:41 PM
<select name="time">
<?php
$times = create_time_range('9:30', '17:30', '30 mins');
foreach ($times as $key => $time)
echo '<option value="' . date('g:i:s', $time) . '">' . date('g:i:s', $time) . '</option>';
?>
</select>

starapple
05-29-2016, 05:28 PM
Saved by a 10-year-old post. Thanks. So much better than Javascript that some users may have turned off.

I posted in response to a thread but it is fairly generic and may be of use to more than a few people.

<?php

/**
* create_time_range
*
* @param mixed $start start time, e.g., 9:30am or 9:30
* @param mixed $end end time, e.g., 5:30pm or 17:30
* @param string $by 1 hour, 1 mins, 1 secs, etc.
* @access public
* @return void
*/
function create_time_range($start, $end, $by='30 mins') {

$start_time = strtotime($start);
$end_time = strtotime($end);

$current = time();
$add_time = strtotime('+'.$by, $current);
$diff = $add_time-$current;

$times = array();
while ($start_time < $end_time) {
$times[] = $start_time;
$start_time += $diff;
}
$times[] = $start_time;
return $times;
}

// create array of time ranges
$times = create_time_range('9:30', '17:30', '30 mins');

// more examples
// $times = create_time_range('9:30am', '5:30pm', '30 mins');
// $times = create_time_range('9:30am', '5:30pm', '1 mins');
// $times = create_time_range('9:30am', '5:30pm', '30 secs');
// and so on

// format the unix timestamps
foreach ($times as $key => $time) {
$times[$key] = date('g:i:s', $time);
}


print '<pre>'. print_r($times, true).'</pre>';
/*
* result
*
Array
(
[0] => 9:30:00
[1] => 10:00:00
[2] => 10:30:00
[3] => 11:00:00
[4] => 11:30:00
[5] => 12:00:00
[6] => 12:30:00
[7] => 1:00:00
[8] => 1:30:00
[9] => 2:00:00
[10] => 2:30:00
[11] => 3:00:00
[12] => 3:30:00
[13] => 4:00:00
[14] => 4:30:00
[15] => 5:00:00
[16] => 5:30:00
)

*/

?>