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?
PfurzAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PfurzAuthor Commented:
the problem was the different time zone!

solution:
		$utc_tz = new DateTimeZone( "UTC");
		$checkin = new DateTime($from,$utc_tz);
		$checkout = new DateTime($to,$utc_tz);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.