Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Convert Day of Year to Day of Month and Month

If you have the day of the year. How can you convert that to day of month and month? For example: The day "144" should be converted to 26th of May. I guess I also have to add the actual year to account for leap years. But I haven't found anything at all.

For example the function mktime() exepects the month, year and day of month.

Anybody some suggestions?

like image 964
Pascal Klein Avatar asked Dec 07 '22 14:12

Pascal Klein


1 Answers

The most reliable and convenient way is to use the DateTime object. You can use DateTime::createFromFormat() static method to create it based on day in the current year:

$date = DateTime::createFromFormat('z', '144');

And because you know have DateTime object in the $date variable, you can perform literally any task you want to. To output the contained date, simply call:

echo $date->format('j. n. Y');

It will print out 24. 5. 2012, because it's leap year and because it indexes days starting from zero (just like array indices).

like image 117
Ondřej Mirtes Avatar answered Dec 10 '22 13:12

Ondřej Mirtes