Jump to content

Time range function not working for times after 11pm


mo

Recommended Posts

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

)

 

*/

 

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;

}

Link to comment
Share on other sites

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; 
} 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.