mo Posted February 13, 2009 Share Posted February 13, 2009 I am using a PHP function I found online used to increment time by a set incremental value. In my case I pass the function a start and end time and 30 minutes as the increment value. If I pass 9am to 10pm the function works fine but if I pass a end time value that is past 11pm, the function does not work. I.e. I am passing 11am to 2am and I only get 11am returned. Code below. <?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 ) */ ?> Quote Link to comment Share on other sites More sharing options...
mo Posted February 13, 2009 Author Share Posted February 13, 2009 Any thoughts, anyone. I think I nedd to add logic to see if the end time is after the current date (after 11:59pm) and perform sone special date related logic. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 13, 2009 Share Posted February 13, 2009 You need to add this line: <?php if(date('a',$start_time) == 'pm' && date('a',$end_time) == 'am') $end_time = strtotime('tomorrow ' . $end); ?> after the third line of the function. <?php function create_time_range($start, $end, $by='30 mins') { $start_time = strtotime($start); $end_time = strtotime($end); if(date('a',$start_time) == 'pm' && date('a',$end_time) == 'am') $end_time = strtotime('tomorrow ' . $end); ?> Ken Quote Link to comment Share on other sites More sharing options...
mo Posted February 13, 2009 Author Share Posted February 13, 2009 This works if my start time is 12pm or later but for my test, my start time is 11am and end time is 2am. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 13, 2009 Share Posted February 13, 2009 Change the if statement to <?php if(date('a',$start_time) == 'pm' && date('a',$end_time) == 'am' || (date('a',$start_time) == 'am' && date('a',$end_time) == 'am' && $start_time > $end_time)) $end_time = strtotime('tomorrow ' . $end); ?> Ken Quote Link to comment Share on other sites More sharing options...
mo Posted February 13, 2009 Author Share Posted February 13, 2009 Thanks, got it working as follows. function create_time_range($start, $end, $by='30 mins') { $start_time = strtotime($start); if(date('a',strtotime($end)) == 'am' && $start > $end){ $end_time = strtotime('tomorrow ' . $end); }else{ $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; } Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 13, 2009 Share Posted February 13, 2009 Did you try your solution. It doesn't work. Ken Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 13, 2009 Share Posted February 13, 2009 There is an easier solution. Just add one line to adjust the end time to the next day if the end time is less than the start time: function create_time_range($start, $end, $by='30 mins') { $start_time = strtotime($start); $end_time = strtotime($end); //MOVE $end_time to next day if less than $start_time if($end_time < $start_time) { $end_time+= 86400; } $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; } Quote Link to comment Share on other sites More sharing options...
mo Posted February 13, 2009 Author Share Posted February 13, 2009 Thanks this change works perfectly. if($end_time < $start_time) { $end_time+= 86400; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 13, 2009 Share Posted February 13, 2009 You're welcome. Please mark topic as solved. Quote Link to comment 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.