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

Pfurz
Pfurz used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial