0

I am posting a start date and time, and an end date and time , for example:

$_POST['start_date'] //this might be 27:04:2013
$_POST['start_time'] //this might be 16:30
$_POST['end_date'] //this might be 29:04:2013
$_POST['end_time'] //this might equal 22:30

I want to create an array of objects for each day that the posted interval of time stretches, would this be a DateInterval object? if so then in the above posted values i want to get an array with the following

[0] 27:04:2013 16:30:00 27:04:2013 23:59:59
[1] 28 04:2014 00:00:00 28 04:2014 23:59:59 
[2] 29:04:2014 00:00:00 29:04:2014 22:30:00
brux
  • 3,197
  • 10
  • 44
  • 78
  • It would be an array of `DateInterval` objects, if what you want is... an array of `DateInterval` objects. We can't tell you what you want. Also, what have you tried? Where are you getting stuck? – salathe Apr 26 '14 at 18:29
  • So, given a start date/time, and an end date/time, you want to create an array where each element represents each day, between and including the start and end dates? – Darragh Enright Apr 26 '14 at 18:30
  • yes an array on DateInterval objects is what i need. – brux Apr 26 '14 at 18:30
  • yes darragh. each element in my array would have the date and the start and end times, for each day that the post duration stretches – brux Apr 26 '14 at 18:31
  • i edited the format of the array i hope to get, sorry if i confused things – brux Apr 26 '14 at 18:33

2 Answers2

2
$start = DateTime::createFromFormat('d:m:Y H:i', '27:04:2013 16:30');
$end   = DateTime::createFromFormat('d:m:Y H:i', '29:04:2013 22:30');
$diff  = $start->diff($end);
$pad   = ($start->format('His') > $end->format('His')) ? 2 : 1;
$days  = $diff->d + $pad;
for ($i = 1; $i <= $days; $i++) {
    if ($i === 1) {
        printf("%s %s<br>", $start->format('d:m:Y H:i:s'), $start->format('d:m:Y 23:59:59'));
    }
    else if ($i === $days) {
        printf("%s %s<br>", $end->format('d:m:Y 00:00:00'), $end->format('d:m:Y H:i:s'));
    }
    else {
        printf("%s %s<br>", $start->format('d:m:Y 00:00:00'), $start->format('d:m:Y 23:59:59'));
    }
    $start->modify('+1 day');
}

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • That's odd about the year. Looks like a PHP bug. Let me see how to workaround that. – John Conde Apr 26 '14 at 18:44
  • Ok. Worked around it. http://codepad.viper-7.com/0xzVCn Gonna have to explore that and see why there are unexpected results. But I recommend in the future using standard datetime formats. Your format only makes working with dates more difficult. – John Conde Apr 26 '14 at 18:49
  • @JohnConde your original code used `H:m` format, instead of the correct `H:i`. – salathe Apr 26 '14 at 18:56
1

It is not clear whether by "interval" you really want a DateInterval object or perhaps a separate start/end DateTime for each day. Either way, the below should serve as a sensible point to go from.

<?php

$start_date = '27:04:2013';
$start_time = '16:30';
$end_date = '29:04:2013';
$end_time = '22:30';

// Date input strings and generate a suitable DatePeriod
$start = DateTime::createFromFormat("d:m:Y H:i", "$start_date $start_time");
$end = DateTime::createFromFormat("d:m:Y H:i", "$end_date $end_time");
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);

foreach ($period as $date) {
    // Get midnight at start of current day
    $date_start = clone $date;
    $date_start->modify('midnight');

    // Get 23:59:59, end of current day
    // (moving to midnight of next day might be good too)
    $date_end = clone $date;
    $date_end->modify('23:59:59');

    // Take care of partial days
    $date_start = max($start, $date_start);
    $date_end = min($end, $date_end);

    // Here you would construct your array of
    // DateTime pairs, or DateIntervals, as you want.
    printf(
        "%s -> %s \n",
        $date_start->format('Y-m-d H:i'),
        $date_end->format('Y-m-d H:i')
    );
}

Which produces the following output:

2013-04-27 16:30 -> 2013-04-27 23:59 
2013-04-28 00:00 -> 2013-04-28 23:59 
2013-04-29 00:00 -> 2013-04-29 22:30

Addendum

If you're lucky enough to be able to use PHP 5.5.0 or above, then the DateTimeImmutable class would make the clone/modify parts much tidier.

salathe
  • 51,324
  • 12
  • 104
  • 132