Why in this code at the output is the result of this?
$ datetime1 = date_create ('2014-03-01');
$ DateTime2 = Date_Create ('2014-03-31');
$ Interval = Date_Diff ($ DateTime1, $ DateTime2);
Echo $ Interval- & gt; Format ('% y% m% d');
----
00 1 2.
----
Where did you come from 2 days?
Change the start date slightly:
$ datetime1 = date_create ('2014-03-02');
$ DateTime2 = Date_Create ('2014-03-31');
$ Interval = Date_Diff ($ DateTime1, $ DateTime2);
Echo $ Interval- & gt; Format ('% y% m% d');
----
00 0 29.
----
just added +1 day, and no longer 1men and 2 days, and 0 months and 29 …
Answer 1, Authority 100%
It seems that I managed to get to the truth. I think this code will explain the cause (time zone is set as Europe / Moscow)
$ sourcedate = strtotime ('2014-03-01');
Echo Date ('Y-M-D', $ Sourcedate), "\ n"; // 2014-03-01
ECHO GMDATE ('Y-M-D', $ SOURCEDATE), "\ n"; // 2014-02-28
$ Sourcedate = StrTotime ('2014-03-31');
Echo Date ('Y-M-D', $ Sourcedate), "\ n"; // 2014-03-31
ECHO GMDATE ('Y-M-D', $ SOURCEDATE), "\ n"; // 2014-03-30
Date extension goes to UTC. Here is an example implementation date_diff for php:
function date_diff ($ Date1, $ DATE2) {
$ current = $ DATE1;
$ DateTime2 = Date_Create ($ DATE2);
$ Count = 0;
While (Date_Create ($ Current) & LT; $ DateTime2) {
$ current = gmdate ("Y-M-D", StrTotime ("+ 1 Day", StrTotime ($ Current)));
$ COUNT ++;
}
RETURN $ COUNT;
}
Therefore, how to solve the problem you can change the code on this:
$ datetime1 = date_create ('2014-03-01 00:00:00 +00');
$ DateTime2 = Date_Create ('2014-03-31 00:00:00 +00');
$ Interval = Date_Diff ($ DateTime1, $ DateTime2);
Echo $ Interval- & gt; Format ('% y% m% d');
And get the correct difference