Gregorian Date or B% #; FAME% :! with this date!

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

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

    The task is as follows: there is a typical ordinary date: July 28, 2006. (July 28, 2006). It is necessary to print in the format: "X year XX months XX days", with the 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, due to stupidity in February (if it wasn’t okay with these leap years), it turns out that the numbers jump :( It’s not enough for the date of the day, but on the contrary brute force. Please help me figure it out. Bright heads.

    Date calculation code itself: Plz don’t minus me. I don’t want the topic for everyone to see, because it’s not a topic, but a helpdesk rather. Someone who really encountered such a problem and knows the solution, please help. UPDATE: If anyone needs it, the answer was prompted to me by ru_php. A working script. Of course, this is a bit not what I wanted, i.e. I wanted to finalize my own, parse it, and not use someone else’s, ready-made script. But still, it works. Link: ru_php / comments
    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,"день","дня","дней");





    Also popular now: