Hope someone can help me. User defines two dates(dd/mm/yy, then want to display 1) Difference in days between the two inputs in dd/mm/yy and 2) the difference in days between the dates. The problem is I always want to show the difference i dd/mm/yy no matter how long the period is but the maximun value I want to show for days is 1825 or 1826(which is five years depending on if it´s one or two leapyears in the given period). Please help. Give me some hlep how to do the second bit. How do I know to stop on 1825 or 1826.

Thank you so much!

    Well, to tell if it's a leap year, you can use
    [man]date[/man]('L', [man]mktime[/man](12, 0, 0, 1,1,'2008'));

    Which would return 1 if 2008 is a leap year (which it is) and 0 if it's not. So you could do something as simple as:

    $startYear = substr($_POST['date1'], -2); // Get the year
    $endYear = substr($_POST['date2'], -2);
    
    $leaps = 0;
    for($i=$startYear; $i<=$endYear; $i++)
    {
        if(date('L', mktime(12,0,0,1,1,$i)))
            $leaps++;
    }

    Now, if $leaps is 2, then you use 1826, otherwise use 1825. You can extrapolate that into a function easily enough.

      date('L', mktime(12, 0, 0, 1,1,'2008'));
      
      $startYear = substr($_POST['from_yr'], -2);
      $endYear = substr($_POST['to_yr'], -2);
      
      $leaps = 0;
      for($i=$startYear; $i<=$endYear; $i++) {
       if(date('L', mktime(12,0,0,1,1,$i))) $leaps++;
      }

      If i input 28 february 2008 - 28 february 2013 i get 5 years 0 monts and 0 days AND 1827 days and one leap years.
      If i input 3 march 2008 - 3 march 2013 i get 5 years 0 monts and 0 days AND 1826 days put only one leap year. <-- Here I only want it to get one leap year.

      Please help.

      Thank you!

        Wrote this quick function to do what you ask.

        <?php
        
        function countLeapYears($startDate=false, $endDate=false)
        {
        	if(!$endDate || (!$startDate && !$endDate))
        		return -1;
        
        $start = strtotime($startDate);
        $end = strtotime($endDate);
        
        $s_year = date('Y', $start);
        $e_year = date('Y', $end);
        
        $leaps = array();
        for($i=$s_year; $i<=$e_year; $i++)
        {
        	if(date('L', mktime(12,0,0,1,1,$i)))
        	{
        		$leaps[$i] = $i;
        	}
        }
        
        // If the start date is LATER than the leap day (2/29), remove it
        if(in_array($s_year, $leaps) && $start > strtotime('02/29/' . $s_year . ' 0:0:0'))
        {
        	unset($leaps[$s_year]);
        }
        
        if(in_array($e_year, $leaps) && $end < strtotime('02/29/' . $e_year . ' 0:0:0'))
        {
        	unset($leaps[$e_year]);
        }
        
        $years = $e_year - $s_year;
        $days = $years * 365;
        $days += count($leaps);
        
        return array('days'=>$days, 'leaps'=>count($leaps));
        }
        
        echo 'February 28, 2008 to February 28, 2013 ( 5 years, 2 leap years ) :: ';
        
        $info = countLeapYears('02/28/2008', '02/28/2013');
        echo $info['days'] . ' days ( ' . $info['leaps'] . ' leaps )<br />';
        
        echo 'March 3, 2008 to March 3, 2013 ( 5 years, 1 leap year ) :: ';
        
        $info = countLeapYears('03/03/2008', '03/03/2013');
        echo $info['days'] . ' days ( ' . $info['leaps'] . ' leaps )<br />';

        Gives me this output:

        February 28, 2008 to February 28, 2013 ( 5 years, 2 leap years ) :: 1827 days ( 2 leaps )
        March 3, 2008 to March 3, 2013 ( 5 years, 1 leap year ) :: 1826 days ( 1 leaps )

          Thank you so very much!

          This date thing is kinda crazy. Wtf is the date 1 year exactly from 29th february?

          Or 5 years with a two leap year period(1827 days) from 1st march 2008 - then you end up on 2nd march 2013.

          What would be your idea on how to implement a function that makes you choose if you want the output in days(not in yy/mm/dd) to be doubled? Designwise...

            JonathanS wrote:

            Wtf is the date 1 year exactly from 29th february?

            It's the day after 1 year exactly from 28 February of course 🙂

              I'm not entirely sure what you're asking Jonathan. 5 years from the first of March 2008 would be the 1st of March 2013. Now, you have to remember that a "year" is an elastic thing (according to some calendars), hence the "leap day". Now, the number of days in that "year" is either 365 or 366. So a year from March 1 2011 to March 1 2012 (the next leap year) is 366 days, but you still refer to March 1 2012 as being one "year" from March 1 2011.

              If you wanted to let peopel choose the output, I'd throw a flag in the parameters (1 or 0, 'days' or 'date', etc.) which specifies whether they want "days" back, or "yy/mm/dd" format. Not too difficult to do.

                Write a Reply...