📜 ⬆️ ⬇️

PHP: FROM_DAYS () port of MySQL

As promised, I quote the code of the FROM_DAYS () function translated from the C language, the get_date_from_daynr function implemented in MySQL.

<?
/ **
*
* @param $ year year number
* @return integer
* /
private function calc_days_in_year ( $ year )
{
return (( $ year & 3 ) == 0 && ( $ year % 100 || ( $ year % 400 == 0 && $ year )))? 366 : 365 ;
}

/ **
*
* @param $ days integer
* @return DateTime
* /
public function from_days ( $ days )
{
$ days_in_month = array ( 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 , 0 );

$ year = 0 ;
$ month = 0 ;
$ day = 0 ;
')
$ days_in_year = 0 ;
$ day_of_year = 0 ;

if ( $ days <= 365 || $ days > = 3652500 )
{
$ year = $ month = $ day = 0 ;
}
else
{
$ year = floor ( $ days * 100/36525 );
$ temp = floor ((( floor (( $ year - 1 ) / 100 ) + 1 ) * 3 ) / 4 );

$ day_of_year = floor (( $ days - $ year * 365 ) - floor (( $ year - 1 ) / 4 ) + $ temp );
while ( $ day_of_year > ( $ days_in_year = $ this -> calc_days_in_year ( $ year )))
{
$ day_of_year - = $ days_in_year ;
$ year ++;
}
$ leap_day = 0 ;

if ( $ days_in_year == 366 )
{
if ( $ day_of_year > 31 + 28 )
{
$ day_of_year -;
if ( $ day_of_year == 31 + 28 )
{
$ leap_day = 1 ;
}
}
}
$ month = 1 ;
for ( $ i = 0 ;
$ day_of_year > $ days_in_month [ $ i ];
$ day_of_year - = $ days_in_month [ $ i ++], $ month ++);

$ day = $ day_of_year + $ leap_day ;
}
return (new DateTime ( "$ year- $ month- $ day" ));
}
?>

Checked function on coincidence with FROM_DAYS () in the range from 1700 to 2300
I tore the function out of my class, so calls like $ this-> calc_days_in_year ($ year) will not work in an independent function.

Enjoy using.

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


All Articles