• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 39
  • Last Modified:

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?
0
Pfurz
Asked:
Pfurz
1 Solution
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now