📜 ⬆️ ⬇️

Gregorian Date or% B; PYM% :! with this date!

Damn, how difficult it is with this date, why are we not having 365 days exactly every year? Why every year we do not have 8760 hours exactly? Why in each year we do not have 31536000 seconds exactly ??

How did I get worn out with these numbers.
Maybe someone knows the solution?

The task is as follows: there is a typical regular date: July 28, 2006. (07.28.2006). It is necessary to output in the format: “ year of months of days”, with calculation of the current day of the month and year. That is (dates for example):

First date: 07.28.2006
Date now: 07/28/2007
Result: 1 year
')
First date: 07.28.2006
Date now: 01/28/2008
Result: 1 year 6 months

First date: 07.28.2006
Date now: 07/28/2008
Result: 2 years

First date: 07.28.2006
Date now: 08/30/2008
Result: 2 years 1 month 2 days


Everything would be fine, everything would be fine, but no, because of stupidity in February (if he is not in tune with these leap years), it turns out that the numbers are jumping :( Light heads.

The date counting code itself:
function pf($n, $form1, $form2, $form5) {
$n = abs($n) % 100;
$n1 = $n % 10;
if ($n > 10 && $n < 20) return $form5;
if ($n1 > 1 && $n1 < 5) return $form2;
if ($n1 == 1) return $form1;
return $form5;
}

$mktime = mktime(0,0,0,7,28,2006);
$nowTime = mktime(0,0,0,7,28,2007); //time();

// CheckGregorian

$y = date("Y",$nowTime);
$minus = 0;

if(!checkdate(2,29,$y)) $minus = 86400;

$mSec = $nowTime - $mktime - $minus;
$yDay = 365 * 86400;

$year = ($mSec - ($mSec % $yDay)) / $yDay; //31557600
$mSec = $mSec - ($year * $yDay);
$month = ($mSec - ($mSec % 2629800)) / 2629800;
$mSec = $mSec - ($month * 2629800);
$day = ($mSec - ($mSec % 86400)) / 86400;

if($year != 0) echo " $year ".pf($year,"","","");
if($month != 0) echo " $month ".pf($month,"","","");
if($day != 0) echo " $day ".pf($day,"","","");

Plz don't minus me. I do not want a topic for everyone to see. This is not a topic, but rather a helpdesk. Someone if really faced such a problem and knows the solution, please help.

UPDATE: If you need someone, then the answer was suggested to me on ru_php. Working script Of course, this is not what I wanted, i.e. I wanted to refine my own, parse it, and not use the already alien, ready-made script. But still, but it works.
Link: ru_php / comments

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


All Articles