Avatar of Pfurz
Pfurz
 asked on

the results of the PHP diff function are wrong when calculating the month and day count at the web server

I calculate the month and day count between 2 dates via the PHP diff-func but i get different results when running the code offline at my dev-pc (PHP Version 5.6.8) and online at the web-server (PHP Version 7.0.24).

my PHP code:

<?php 

for($m = 1; $m < 6; $m++)	//  months
{	
	for($d = 1; $d < 4; $d++)	// days
	{	
		$date1 = "2018-0".$m."-01";
		$date2 = "2018-0".($m+1)."-0".$d;
		
		$from = date("Y-m-d", strtotime($date1));
		$to   = date("Y-m-d", strtotime($date2));
		
		$checkin = new DateTime($from);
		$checkout = new DateTime($to);
		
		$dif = $checkin->diff($checkout);
		
		$years = $dif->y;
		$months = $dif->m;
		$days = $dif->d;
		
		echo "time period from $date1 to $date2 = $years years, $months months, $days days<br>";
	}
	echo "---<br>";
}

?>

Open in new window


the result at my dev-pc (PHP Version 5.6.8):

time period from 2018-01-01 to 2018-02-01 = 0 years, 1 months, 0 days
time period from 2018-01-01 to 2018-02-02 = 0 years, 1 months, 1 days
time period from 2018-01-01 to 2018-02-03 = 0 years, 1 months, 2 days
---
time period from 2018-02-01 to 2018-03-01 = 0 years, 1 months, 0 days
time period from 2018-02-01 to 2018-03-02 = 0 years, 1 months, 1 days
time period from 2018-02-01 to 2018-03-03 = 0 years, 1 months, 2 days
---
time period from 2018-03-01 to 2018-04-01 = 0 years, 1 months, 0 days
time period from 2018-03-01 to 2018-04-02 = 0 years, 1 months, 1 days
time period from 2018-03-01 to 2018-04-03 = 0 years, 1 months, 2 days
---
time period from 2018-04-01 to 2018-05-01 = 0 years, 1 months, 0 days
time period from 2018-04-01 to 2018-05-02 = 0 years, 1 months, 1 days
time period from 2018-04-01 to 2018-05-03 = 0 years, 1 months, 2 days
---
time period from 2018-05-01 to 2018-06-01 = 0 years, 1 months, 0 days
time period from 2018-05-01 to 2018-06-02 = 0 years, 1 months, 1 days
time period from 2018-05-01 to 2018-06-03 = 0 years, 1 months, 2 days
---

the result at the web-server (PHP Version 7.0.24):

time period from 2018-01-01 to 2018-02-01 = 0 years, 1 months, 0 days
time period from 2018-01-01 to 2018-02-02 = 0 years, 1 months, 1 days
time period from 2018-01-01 to 2018-02-03 = 0 years, 1 months, 2 days
---
time period from 2018-02-01 to 2018-03-01 = 0 years, 0 months, 28 days
time period from 2018-02-01 to 2018-03-02 = 0 years, 0 months, 29 days
time period from 2018-02-01 to 2018-03-03 = 0 years, 0 months, 30 days
---
time period from 2018-03-01 to 2018-04-01 = 0 years, 1 months, 3 days
time period from 2018-03-01 to 2018-04-02 = 0 years, 1 months, 4 days
time period from 2018-03-01 to 2018-04-03 = 0 years, 1 months, 5 days
---
time period from 2018-04-01 to 2018-05-01 = 0 years, 0 months, 30 days
time period from 2018-04-01 to 2018-05-02 = 0 years, 1 months, 0 days
time period from 2018-04-01 to 2018-05-03 = 0 years, 1 months, 1 days
---
time period from 2018-05-01 to 2018-06-01 = 0 years, 1 months, 1 days
time period from 2018-05-01 to 2018-06-02 = 0 years, 1 months, 2 days
time period from 2018-05-01 to 2018-06-03 = 0 years, 1 months, 3 days

Can anyone explain me why the web-server calculate the 2 dates wrong and how to fix this?
PHP* diff

Avatar of undefined
Last Comment
Pfurz

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Pfurz

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck