3

I have a function that calculate how many days in 1 year and I got it to work from Monday to Saturday by changing the $week variable from :

Monday - 6:Saturday, but it will not work when I put 7: Sunday.

can anyone help out. am I missing any logic?

$year = 2016;
$newyear = $year;
$week = 0;
$day = 0;
$mo = 1;
$days = array();
$i = 1;

        while ($week != 7) { // here is where I change the 1-7 for days
          $day++;
          $week = date("w", mktime(0, 0, 0, $mo,$day, $year));
        }

        array_push($days,date("r", mktime(0, 0, 0, $mo,$day, $year)));
        while ($newyear == $year) {
          $x =  strtotime(date("r", mktime(0, 0, 0, $mo,$day, $year)) . "+" . $i . " week");
          $i++;
          if ($year == date("Y",$x)) {
            array_push($days,date("r", $x));
          }
          $newyear = date("Y",$x);
        }

        print count($days);

thank you for the help cheers! and would be possible to count immediately 2 years of total days like example :

I have a date which is 11 january 2016 that is monday, and I wanted to know how many days are there from 11 january 2016 to 11january 2018, how many mondays are there.

thank you!

Venkat.R
  • 7,420
  • 5
  • 42
  • 63
Boby
  • 63
  • 1
  • 2
  • 11
  • 1
    weeks are 0 (sunday) to 6 (saturday), see [manual](http://php.net/manual/en/function.date.php) – kamal pal Jan 11 '16 at 08:37
  • 1
    Working with time in php, or any other language, is a can of worms. I'd suggest trying out [Carbon](https://github.com/briannesbitt/Carbon) it was specifically made for these sort of things. – Andrei Jan 11 '16 at 08:41
  • Check these SO questions: [Finding the number of days between two dates](http://stackoverflow.com/questions/2040560/finding-the-number-of-days-between-two-dates) | [How to count days between two dates in PHP?](http://stackoverflow.com/questions/3653882/how-to-count-days-between-two-dates-in-php) | [find number of mondays or tuesdays between two dates?](http://stackoverflow.com/questions/1653891/how-to-find-number-of-mondays-or-tuesdays-between-two-dates) | [get all mondays within date range](http://stackoverflow.com/questions/7061802/php-function-for-get-all-mondays-within-date-range) – StoYan Jan 11 '16 at 08:52
  • Since you are new to StackOverflow: be sure to accept an answer if it resolves your question, thank you – Clay Jan 11 '16 at 09:47

2 Answers2

3

Using DateTime/DateInterval functions:

$datetime1 = new DateTime('2018-01-11');
$datetime2 = new DateTime('2016-01-11');
$interval = $datetime1->diff($datetime2);
echo floor($interval->format('%a days')/7); // 104

Or using strtotime...

You can also do this:

$startDate = strtotime('2016-01-11');
$endDate = strtotime('2018-01-11');
$totalWeeks = (($endDate - $startDate)/86400)/7;
echo floor($totalWeeks); // rounds to 104

Read more here:

DateDiff - http://php.net/manual/en/datetime.diff.php

DateInterval::format - http://php.net/manual/en/dateinterval.format.php

Update... how to 'debug' this easily for the day counting:

<?php

$startDate = strtotime('2016-01-11');
$endDate = strtotime('2018-01-11');
$currentDate = $startDate;

$count = 0;
while ($currentDate <= $endDate) {
    echo date('r', $currentDate) . "\n";
    $currentDate = strtotime('+1 week', $currentDate);
    if ($currentDate<=$endDate) {
        $count++;
    }
}

echo $count . "\n";
Clay
  • 4,700
  • 3
  • 33
  • 49
  • I will need to know only specific day like how many Mondays are there for those 2years range... – Boby Jan 11 '16 at 09:14
  • Take a look at my new answer to see if it answers your question and let me know if that works for you – Clay Jan 11 '16 at 09:19
  • Are you looking for the count between the Mondays? or do you want to include the first Monday and the last Monday? – Clay Jan 11 '16 at 09:23
  • Hi Clayton! yeah it will work on Sundays, but if I wanted to know for Monday? or Tuesday? is there any options where I can change the value like to 1 then it will tell me how many mondays are there, or 2 then it will tell me tuesdays are there? thank you! – Boby Jan 11 '16 at 09:25
  • I am trying to build a small function so when I input the date, and lets say the date is monday, then count how many mondays are there in 2years, then if the date is friday, then how many fridays are there in 2years, something like that – Boby Jan 11 '16 at 09:26
  • You can change what I posted to use a different date, then based on whatever that date is, it will get the number. – Clay Jan 11 '16 at 09:28
  • If someone enters 2016-01-11, do you want to know how many Fridays there are? or only if someone enters 2016-01-15? – Clay Jan 11 '16 at 09:34
  • to summarize that means , if I use date of 8january2016 which is friday, it will spit out how many fridays are there in 2years time using your function? if someone enters 15january2016, then find out how many fridays are there in 2years time :) – Boby Jan 11 '16 at 09:35
  • I've added a 'debug' method that will show you the output of all the dates and the final count at the end. You will have to test it to verify it works for you. It works for me. You may need to adjust if you need include the beginning/ending days. – Clay Jan 11 '16 at 09:39
  • its working, thank you so much! you made my day been trying for 1whole day now :) – Boby Jan 11 '16 at 09:53
2
$date_1 =  strtotime("2016-01-11");
$date_2 = strtotime("2018-01-11");
$datediff = $date_2 - $date_1;
echo floor($datediff/(60*60*24));

Modified :

You can find any day of week between two dates. just change value of $days[0];

For Monday :

<?php
$date_1 = $from = strtotime('2016-01-11');
$date_2 = strtotime('2018-01-11');
$days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
$count = 0;
while ($date_1 < $date_2) {
  if(date('l', $date_1) == $days[0]);
  {
    $count++;   
  }
  $date_1 += 7 * 24 * 3600;
}
echo "From : ".date('Y-m-d',$from)."  To : ".date('Y-m-d',$date_2)."  has $count  $days[0]";
?>

OUTPUT :

From : 2016-01-11 To : 2018-01-11 has 105 Monday
Monty
  • 1,110
  • 7
  • 15