K
K
KoRNeT46RuS2018-03-23 15:30:11
PHP
KoRNeT46RuS, 2018-03-23 15:30:11

Why does the function count 0 months between February 1 and 28, and 1 month between March 1 and 31?

$datetime1 = new \DateTime(//начало месяца);
$datetime2 = new \DateTime(//конец месяца);
$interval = $datetime1->diff($datetime2);
$interval = $interval->format('%m');

between the first and last of February 0 months, and between 1 and 31 March 1 month. Why?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
j-ker, 2018-03-24
@KoRNeT46RuS

For all major versions the result is the same and clumsy. Hmm...
https://3v4l.org/KXYvi
And this is what is written in the documentation:
blabalbalaaabalablala a lot of letters, yeah, here:
set date_default_timezone_set('UTC');
and everything starts to work not as before, but as it should:
https://3v4l.org/ha3O9
in the extreme example, the first two intervals within the month are zero on all versions,
and the third example from February 1 to March 1 = 1 on all versions
, and it's all connected with walking around the clock between zones (respectively, on the extreme dates, these UNCOUNTERED +-hours are zonal and, as I understand it, give a deviation from the norm)
Thanks for the interesting question.

A
Alexander, 2018-03-23
@Minifets

What version of php?
On 7.1 between March 1 and 31 0 months, as on 5.6

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question