How to calculate the number of days since the beginning of the year using DateTime when accounting for leap years?

I am trying to use a DateTime object to calculate the current date from the number of days since January 1st. Leap years are very important here. This does not seem to take leap years into account.

Here is my code:

 $date = DateTime::createFromFormat('z Y', '59 2016'); echo $date->format('n/j/Y')."\n"; die(); 
+5
source share
1 answer

Turns out this is a PHP bug from 2012, which I just found when I asked this question:

https://bugs.php.net/bug.php?id=62476

This is annoying.

Here is a workaround:

 $date = DateTime::createFromFormat('m/d/Y', '01/01/2016'); $date->add(date_interval_create_from_date_string('59 days')); echo $date->format('m/d/Y')."\n"; 
+6
source

Source: https://habr.com/ru/post/1212561/


All Articles