8

I want to calculate all Sunday's between given two dates. I tried following code. It works fine if days are less but if i enter more days. It keeps processing and Maximum execution time exceeds i changed the time but it even keeps processing even execution time is 200sec.

code is

<?php
$one="2013-01-01";
$two="2013-02-30";

$no=0;
for($i=$one;$i<=$two;$i++)
{

    $day=date("N",strtotime($i));
    if($day==7)
    {
    $no++;
    }
}
echo $no;

?>

please help.

8

3 Answers 3

34

John Conde's answer is correct, but here is a more efficient and mathy solution:

$start = new DateTime('2013-01-06');
$end = new DateTime('2013-01-20');
$days = $start->diff($end, true)->days;

$sundays = intval($days / 7) + ($start->format('N') + $days % 7 >= 7);

echo $sundays;

Let me break it down for you.

$start = new DateTime('2013-01-06');
$end = new DateTime('2013-01-20');

First, create some DateTime objects, which are powerful built-in PHP objects meant for exactly this kind of problem.

$days = $start->diff($end, true)->days;

Next, use DateTime::diff to find the difference from $start to $end (passing true here as the second parameter ensures that this value is always positive), and get the number of days between them.

$sundays = intval($days / 7) + ($start->format('N') + $days % 7 >= 7);

Here comes the big one - but it's not so complicated, really. First, we know there is one Sunday for every week, so we have at least $days / 7 Sundays to begin with, rounded down to the nearest int with intval.

On top of that, there could be a Sunday in a span of time less than a week; for example, Friday to Monday of the next week contains 4 days; one of them is a Sunday. So, depending on when we start and end, there could be another. This is easy to account for:

  • $start->format('N') (see DateTime::format) gives us the ISO-8601 day of the week for the start date, which is a number from 1 to 7 (1 is Monday, 7 is Sunday).
  • $days % 7 gives us the number of leftover days that don't divide evenly into weeks.

If our starting day and the number of leftover days add up to 7 or more, then we reached a Sunday. Knowing that, we just have to add that expression, which will give us 1 if it's true or 0 if it's false, since we're adding it to an int value.

And there you have it! The advantage of this method is that it doesn't require iterating over every day between the given times and checking to see if it's a Sunday, which will save you a lot computation, and also it will make you look really clever. Hope that helps!

10
  • 2
    This part isn't true Since $days and 7 are both int values, this will always be rounded down to the nearest int. You have to use a round function like floor() to round it, PHP won't do it for you, try for example $end = new DateTime('2013-02-20'); and the output will be decimal.
    – HamZa
    Apr 8, 2013 at 20:52
  • 2
    You're absolutely right, whoops! Forgot I was talking about PHP for a second. Apr 8, 2013 at 20:59
  • @QuinnStrahl If want to find first occurrence of any day(lets say sunday) so how to find that?
    – Er.KT
    Sep 18, 2014 at 10:22
  • 2
    @Er.KT The algorithm works for any day except for this last part: If our starting day and the number of leftover days add up to 7 or more, then we reached a Sunday. You should only have to change this expression: $start->format('N') + $days % 7 >= 7 to something like $start->format('N') + $days % 7 >= $ISO8601NumberForDesiredDayOfWeek. As mentioned above, the ISO-8601 number for a day of the week is a number between 1 and 7, with Monday being 1 and Sunday being 7. Sep 18, 2014 at 19:52
  • 4
    Hi,thanks for the solution.Can you make it dynamic like if I want to know how many Mondays are there then what to do.
    – Tanvir
    Feb 3, 2015 at 15:58
10
<?php
$no = 0;
$start = new DateTime('2013-01-01');
$end   = new DateTime('2013-04-30');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt)
{
    if ($dt->format('N') == 7)
    {
        $no++;
    }
}
echo $no;

See it in action

2
  • thank you. it works. so it was not working because string was not incremented properly? Apr 8, 2013 at 18:31
  • Basically yes. Working with strings, especially dates, is best left to functionality designed to do that. DateTime is designed specifically to work with dates and as you can see makes it easy to do.
    – John Conde
    Apr 8, 2013 at 18:33
2

Here is a solution if you want the Sundays in a specific date range.

function dateRange($begin, $end, $interval = null)
{
  $begin = new DateTime($begin);
  $end = new DateTime($end);

  $end = $end->modify('+1 day');
  $interval = new DateInterval($interval ? $interval : 'P1D');

  return iterator_to_array(new DatePeriod($begin, $interval, $end));
}

/* define date range */
$dates = dateRange('2018-03-01', '2018-03-31');

/* define weekdays */
$weekends = array_filter($dates, function ($date) {
  $day = $date->format("N");
  return $day === '6' || $day === '7';
});

/* weekdays output */
foreach ($weekends as $date) {
  echo $date->format("D Y-m-d") . "</br>";
}

/* define sundays */
$sundays = array_filter($dates, function ($date) {
  return $date->format("N") === '7';
});

/* sundays output */
foreach ($sundays as $date) {
echo $date->format("D Y-m-d") . "</br>";
}

/* define mondays */
$mondays = array_filter($dates, function ($date) {
 return $date->format("N") === '1';
});

/* mondays output */
foreach ($mondays as $date) {
echo $date->format("D Y-m-d") . "</br>";
}

Just change the number for any days you want in your output:

Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7

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.