10

Given the following dates:

6/30/2010 - 7/6/2010

and a static variable:

$h = 7.5

I need to create an array like:

Array ( [2010-06-30] => 7.5 [2010-07-01] => 7.5 => [2010-07-02] => 7.5 => [2010-07-05] => 7.5 => [2010-07-06] => 7.5) 

Weekend days excluded.

No, it's not homework...for some reason I just can't think straight today.

1
  • 1
    Enumerate the dates, using getdate(iter_date)["wday"] to skip the week-end (wday: 0, or 6).
    – pascal
    Jul 28, 2010 at 12:23

4 Answers 4

26

For PHP >= 5.3.0, use the DatePeriod class. It's unfortunately barely documented.

$start = new DateTime('6/30/2010');
$end = new DateTime('7/6/2010');
$oneday = new DateInterval("P1D");

$days = array();
$data = "7.5";

/* Iterate from $start up to $end+1 day, one day in each iteration.
   We add one day to the $end date, because the DatePeriod only iterates up to,
   not including, the end date. */
foreach(new DatePeriod($start, $oneday, $end->add($oneday)) as $day) {
    $day_num = $day->format("N"); /* 'N' number days 1 (mon) to 7 (sun) */
    if($day_num < 6) { /* weekday */
        $days[$day->format("Y-m-d")] = $data;
    } 
}    
print_r($days);
4
  • When I run this I get Array ( [2010-06-30] => 7.5 [2010-07-01] => 7.5 [2010-07-02] => 7.5 [2010-07-05] => 7.5 ) It's missing the last day, 7/6. Am I missing something simple?
    – Jason
    Jul 28, 2010 at 12:43
  • Seems like changing $end = '7/6/2010'; to $end = date ( 'm/d/Y' , strtotime ( '+1 day' , strtotime ('7/06/2010') ) ); Gets me what I'm looking for.
    – Jason
    Jul 28, 2010 at 13:21
  • Yes, the DatePeriod appearently stops before the end-date. My bad, fixed.
    – gnud
    Jul 29, 2010 at 10:43
  • If we dont now the end date, how to get date range for n days excluding weekends. Feb 27, 2013 at 4:29
5

The simplest method:

$start = strtotime('6/30/2010');
$end = strtotime('7/6/2010');
$result = array();
while ($start <= $end) {
    if (date('N', $start) <= 5) {
        $current = date('m/d/Y', $start);
        $result[$current] = 7.5;
    }
    $start += 86400;
}
print_r($result);

UPDATE: Forgot to skip weekends. This should work now.

2
  • 1
    Watch out for daylight savings
    – Mark Baker
    Jul 28, 2010 at 12:33
  • 1
    Good answer, for place without daylight saving :)
    – Shiro
    Aug 1, 2013 at 3:21
1

This is gnud's answer but as a function (also added an option to exclude the current day from the calculation):

(examples below)

public function getNumberOfDays($startDate, $endDate, $hoursPerDay="7.5", $excludeToday=true)
{
    // d/m/Y
    $start = new DateTime($startDate);
    $end = new DateTime($endDate);
    $oneday = new DateInterval("P1D");

    $days = array();

    /* Iterate from $start up to $end+1 day, one day in each iteration.
    We add one day to the $end date, because the DatePeriod only iterates up to,
    not including, the end date. */
    foreach(new DatePeriod($start, $oneday, $end->add($oneday)) as $day) {
        $day_num = $day->format("N"); /* 'N' number days 1 (mon) to 7 (sun) */
        if($day_num < 6) { /* weekday */
            $days[$day->format("Y-m-d")] = $hoursPerDay;
        } 
    }    

    if ($excludeToday)
        array_pop ($days);

    return $days;       
}

And to use it:

$date1 = "2012-01-12";
$date2 = date('Y-m-d'); //today's date  

$daysArray = getNumberOfDays($date1, $date2);

echo 'hours: ' . array_sum($daysArray);
echo 'days: ' . count($daysArray);
1
  • hello @jarrod, I want minutes toooo.
    – User1988
    Mar 1, 2012 at 11:51
0

This is OOP approach, just in case. It returns an array with all of dates, except the weekends days.

    class Date{
        public function getIntervalBetweenTwoDates($startDate, $endDate){
            $period = new DatePeriod(
                 new DateTime($startDate),
                 new DateInterval('P1D'),
                 new DateTime($endDate)
            );
            $all_days = array();$i = 0;
            foreach($period as $date) {
                if ($this->isWeekend($date->format('Y-m-d'))){
                    $all_days[$i] = $date->format('Y-m-d');
                    $i++;
                }
            }
            return $all_days;
        }
        public function isWeekend($date) {
            $weekDay = date('w', strtotime($date));
            if (($weekDay == 0 || $weekDay == 6)){
                return false;
            }else{
                return true;
            }
        }
    }
    $d = new Date();
    var_dump($d->getIntervalBetweenTwoDates('2015-08-01','2015-08-08'));
1
  • What if you have two same dates? var_dump($d->getIntervalBetweenTwoDates('2015-08-01','2015-08-01'))
    – SuperManSL
    Jul 21, 2015 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.