Posted By

bachya on 11/25/09


Tagged

php time range


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

bachya
StrawMan
joethermal


Time Ranges Array


 / Published in: PHP
 

This function takes a start time, an end time, and an interval ("step size"). It returns an array of times separated by that interval between the start time and end time (similar to what Google Calendar does). The keys of the array are the number of seconds between their times and the start time.

  1. static public function timeIntervals($start_time, $end_time, $interval)
  2. {
  3. $start_formatted = strtotime($start_time);
  4. $end_formatted = strtotime($end_time);
  5. $interval = $interval * 60;
  6. $times = array();
  7.  
  8. for ($i = $start_formatted; $i < ($end_formatted); $i += $interval)
  9. {
  10. $seconds = $i - strtotime(date('Y-m-d'));
  11. $times[$seconds] = date('g:i a', $i);
  12. }
  13.  
  14. return $times;
  15. }

Report this snippet  

You need to login to post a comment.