-1

Why this while loop of dates stops after increasing up to 3 days only; I want it to run for a month.

$rtf = "1/1/2015 10:00:00 AM"; //Required time from
$rtt = "1/30/2015 5:00:00 PM"; //Required time to
$atArr = []; // Available times array

$t = $rtf;
while ($t <= $rtt) {
    $atArr[date('n/j/Y 8:00:00 ', (strtotime($t))) . 'AM'] = date('n/j/Y 5:00:00 ', (strtotime($t))) . 'PM';
    $t = date('n/j/Y g:i:s A', (strtotime('+1 Day', strtotime($t))));
}

echo "<br><br> --------- TIMES ARRAY INIT --------- <br>";
var_dump($atArr);
  • Your code is ignoring the `0` in `$rtt = "1/30/2015 5:00:00 PM";` -- To test this, try `$rtt = "1/9/2015 5:00:00 PM";` -- Your code is "seeing" `$rtt = "1/3/2015 5:00:00 PM";` – Zak Jun 20 '22 at 16:55
  • Your finding is correct but problem is more than this. I tried with different $rtt values; every time it behaves strange. Can't figure out what's wrong with it. – Muhammad Khalil Raza Jun 20 '22 at 18:42
  • 1
    Because those are _strings_ and `"1/4/2015" > "1/30/2015"`. https://www.php.net/manual/en/class.datetime – Sammitch Jun 20 '22 at 19:53

1 Answers1

0

The DateTime class makes working with dates like this much simpler and less prone to mistakes. The following should yield the desired results:

# Your start and end dates
$dates=(object)array(
    'start' =>  '1/1/2015 10:00:00 AM',
    'end'   =>  '1/30/2015 5:00:00 PM'
);
# The input and output format for all dates used
$format='n/j/Y g:i:s A';

#create the start and end datetime objects and an interval of 1day ( 'P1D' )
$start=DateTime::createFromFormat( $format, $dates->start );
$end=DateTime::createFromFormat( $format, $dates->end );
$interval=new DateInterval('P1D');

$atArr = [];
# simple loop that adds the interval until it reaches the end date
while( $start < $end ){
    $atArr[]=$start->format( $format );
    $start->add( $interval );
}
    
    

printf('<pre>%s</pre>',print_r($atArr,true));

The result:

Array
(
    [0] => 1/1/2015 10:00:00 AM
    [1] => 1/2/2015 10:00:00 AM
    [2] => 1/3/2015 10:00:00 AM
    [3] => 1/4/2015 10:00:00 AM
    [4] => 1/5/2015 10:00:00 AM
    [5] => 1/6/2015 10:00:00 AM
    [6] => 1/7/2015 10:00:00 AM
    [7] => 1/8/2015 10:00:00 AM
    [8] => 1/9/2015 10:00:00 AM
    [9] => 1/10/2015 10:00:00 AM
    [10] => 1/11/2015 10:00:00 AM
    [11] => 1/12/2015 10:00:00 AM
    [12] => 1/13/2015 10:00:00 AM
    [13] => 1/14/2015 10:00:00 AM
    [14] => 1/15/2015 10:00:00 AM
    [15] => 1/16/2015 10:00:00 AM
    [16] => 1/17/2015 10:00:00 AM
    [17] => 1/18/2015 10:00:00 AM
    [18] => 1/19/2015 10:00:00 AM
    [19] => 1/20/2015 10:00:00 AM
    [20] => 1/21/2015 10:00:00 AM
    [21] => 1/22/2015 10:00:00 AM
    [22] => 1/23/2015 10:00:00 AM
    [23] => 1/24/2015 10:00:00 AM
    [24] => 1/25/2015 10:00:00 AM
    [25] => 1/26/2015 10:00:00 AM
    [26] => 1/27/2015 10:00:00 AM
    [27] => 1/28/2015 10:00:00 AM
    [28] => 1/29/2015 10:00:00 AM
    [29] => 1/30/2015 10:00:00 AM
)
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46