Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php date interval/date period not behaving as expected

After doing some research into Dateperiod, it turns out it by default excludes the enddate, even though it doesn't explicitly state that anywhere in the PHP manual. I also didn't notice any option for including it. The only option there seems to be is the option to exclude the start date. Has anyone else run across this?

like image 760
jz3 Avatar asked Feb 27 '12 20:02

jz3


1 Answers

you forgot the time

  $start_date = '02/28/2012 00:00:00';
  $end_date = '02/29/2012 23:59:59';
  $intrDate = '1D';

  $start = new \DateTime($start_date);
  $end = new \DateTime($end_date);
  $interval = new \DateInterval('P'.$intrDate);
  $period = new \DatePeriod($start, $interval, $end);

  print_r($start_date);
  print_r($end_date);
  print_r($period);

  foreach ($period as $day) {
          $dates[] = array(
              'eventID' => $event_id, 
              'date' => $day->format('Y-m-d'), 
              'max' => $data['numAttending']);
      }

  print_r($dates);
  exit;

this output:

Array
(
    [0] => Array
        (
            [eventID] => 
            [date] => 2012-02-28
            [max] => 
        )

    [1] => Array
        (
            [eventID] => 
            [date] => 2012-02-29
            [max] => 
        )

)

without the time, you will get:

Array
(
    [0] => Array
        (
            [eventID] => 
            [date] => 2012-02-28
            [max] => 
        )

)
like image 185
EscoMaji Avatar answered Nov 15 '22 06:11

EscoMaji