1

I'm trying to calculate the number of weeks between two dates. The code below has a result of 3 weeks. However, it is really 4 weeks. Why is it calculating incorrectly and what is the solution?

I'm curious to know why this specific code isn't working but would also like to know if there is a better way of doing it.

I'm running PHP version 7.2. Below is the code I'm using:

$HowManyWeeks = date( 'W', strtotime( 2019-04-21 23:59:00 ) ) - date( 'W', strtotime( 2019-03-25 00:00:00 ) );

The value of $HowManyWeeks should be 4 but it is displaying as 3.

Also, when I try that code at https://phpfiddle.org/ it gives an error of:

Line : 2 -- syntax error, unexpected '23' (T_LNUMBER), expecting ',' or ')'

But it displays '3' without any error when running it on my server.

Thanks,

Tim

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
Tim M
  • 306
  • 3
  • 18
  • Possible duplicate of [How to calculate the difference between two dates using PHP?](https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – miken32 May 01 '19 at 19:50

3 Answers3

5

The dates you pass to strtotime need to be enclosed in quotes. And the correct answer is indeed 3, as there are 3 weeks, 6 days, 23 hours and 59 minutes between the two times. Try this:

$HowManyWeeks = date( 'W', strtotime( '2019-04-21 23:59:00' ) ) - date( 'W', strtotime( '2019-03-25 00:00:00' ) );
echo $HowManyWeeks;

As has been pointed out, this will only work when the weeks are in the same year. It's easier to use DateTime objects as in @MiroslavGlamuzina answer or you can simply divide the strtotime difference by 604800 (seconds in a week); you can then take the floor or ceil if required to convert to an integer value:

$HowManyWeeks = (strtotime( '2019-04-21 23:59:00' ) - strtotime( '2019-03-25 00:00:00' )) / 604800;
echo $HowManyWeeks;

Output:

3.9939484126984

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
  • This code will only work for dates within a given year. For example; Jan 1 2018 and Jan 9 2017 will only result in a one week difference. – Miroslav Glamuzina Apr 30 '19 at 04:22
  • 1
    @MiroslavGlamuzina you are absolutely correct. I've updated with something more robust. But +1 to you... – Nick Apr 30 '19 at 08:44
4

You can use DateTime() to achieve this:

$date1 = new DateTime('2017-04-30');
$date2 = new DateTime('2019-04-30');
// I have left the remainer. You may need to round up/down. 
$differenceInWeeks = $date1->diff($date2)->days / 7;
print_r($differenceInWeeks);

Hope this helps,

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Miroslav Glamuzina
  • 4,472
  • 2
  • 19
  • 33
2

The value of $HowManyWeeks should be 4

Why? please note in php a week is begin with Monday, and I count in the calendar, it's exactly 3.

If you need 4 (Sunday as the first day of week), check the day of week

$time1 = strtotime('2019-04-21 23:59:00');
$week1 = idate('W', $time1);
if(idate('w', $time1) == 0) # Sunday, next week
    $week1++;
...
shingo
  • 18,436
  • 5
  • 23
  • 42