Time Ranges Array


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.