SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast bokz_42's Avatar
    Join Date
    Sep 2006
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    date computation

    hi,

    just wanted to know a work around to this problem.
    lets say $a='2004-01-01'; and $b='2008-07-30';

    how can i compute for its difference and format it in this fashion
    4 years 7 months 30 days.

    thanks in advance!

  2. #2
    SitePoint Addict devil cat's Avatar
    Join Date
    Apr 2003
    Location
    Reno
    Posts
    344
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I don't know if there's a function in PHP for that, but you can convert the dates to timestamps using mktime() and then use simple math on the difference to find out years, etc.

  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Location
    Melbourne
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have a look in PEAR's Date library.
    There's Calc.php that contains date calculation functions.

  4. #4
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of PHP you can use Mysql Query. You will find lots of mysql date functions to make your life easier.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Calc the diff either using the database or php, then feed it to
    http://aidanlister.com/repos/v/Duration.php

  6. #6
    SitePoint Member
    Join Date
    Jul 2008
    Location
    Auckland, New Zealand
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    class Format
    {
    public static function until($from, $to, $do_more = false)
    {
    // array of time period chunks
    $chunks = array(
    array(60 * 60 * 24 * 365 , ('year') , ('years')),
    array(60 * 60 * 24 * 30 , ('month') , ('months')),
    array(60 * 60 * 24 * 7, ('week') , ('weeks')),
    array(60 * 60 * 24 , ('day') , ('days')),
    array(60 * 60 , ('hour') , ('hours')),
    array(60 , ('minute') , ('minutes')),
    array(1 , ('second') , ('seconds')),
    );

    $until = $to - $from;

    for ($i = 0, $j = count($chunks); $i < $j; $i++) {
    $seconds = $chunks[$i][0];
    $name = $chunks[$i][1];
    $names = $chunks[$i][2];

    if ( 0 != $count = floor($until / $seconds) )
    break;
    }

    $print = sprintf('&#37;1$d %2$s', $count, $count == 1 ? $name : $names);

    if ( $do_more && $i + 1 < $j) {
    $seconds2 = $chunks[$i + 1][0];
    $name2 = $chunks[$i + 1][1];
    $names2 = $chunks[$i + 1][2];
    if ( 0 != $count2 = floor( ($until - $seconds * $count) / $seconds2) )
    $print .= sprintf(', %1$d %2$s', $count2, ($count2 == 1) ? $name2 : $names2);
    }
    return $print;
    }
    }

    Hopefully this helps. Convert both your dates to timestamps and call format::until($to, $from) and you'll get a result like what you wanted.

  7. #7
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    i don't remember where i got this code. I think it can be help you.
    PHP Code:
    <?php
    /*
        License:  do whatever you want with this code
        
        Disclaimer:  This code works well on my system, but may not on yours.  Use
        with circumspection and trepidation.  If this code blows up your system,
        I recommend vituperation.
    */



    /*
        function smoothdate simply takes a year, month, and a day, and
        concatenates them in the form YYYYMMDD
        
        the function date_difference uses this function
    */

    function smoothdate ($year$month$day)
    {
        return 
    sprintf ('%04d'$year) . sprintf ('%02d'$month) . sprintf ('%02d'$day);
    }


    /*
        function date_difference calculates the difference between two dates in
        years, months, and days.  There is a ColdFusion funtion called, I
        believe, date_diff() which performs a similar function.
        
        It does not make use of 32-bit unix timestamps, so it will work for dates
        outside the range 1970-01-01 through 2038-01-19.  This function works by
        taking the earlier date finding the maximum number of times it can
        increment the years, months, and days (in that order) before reaching
        the second date.  The function does take yeap years into account, but does
        not take into account the 10 days removed from the calendar (specifically
        October 5 through October 14, 1582) by Pope Gregory to fix calendar drift.
        
        As input, it requires two associative arrays of the form:
        array (    'year' => year_value,
                'month' => month_value.
                'day' => day_value)
        
        The first input array is the earlier date, the second the later date.  It
        will check to see that the two dates are well-formed, and that the first
        date is earlier than the second.
        
        If the function can successfully calculate the difference, it will return
        an array of the form:
        array (    'years' => number_of_years_different,
                'months' => number_of_months_different,
                'days' => number_of_days_different)
                
        If the function cannot calculate the difference, it will return FALSE.
        
    */

    function date_difference ($first$second)
    {
        
    $month_lengths = array (312831303130313130313031);

        
    $retval FALSE;

        if (    
    checkdate($first['month'], $first['day'], $first['year']) &&
                
    checkdate($second['month'], $second['day'], $second['year'])
            )
        {
            
    $start smoothdate ($first['year'], $first['month'], $first['day']);
            
    $target smoothdate ($second['year'], $second['month'], $second['day']);
                                
            if (
    $start <= $target)
            {
                
    $add_year 0;
                while (
    smoothdate ($first['year']+ 1$first['month'], $first['day']) <= $target)
                {
                    
    $add_year++;
                    
    $first['year']++;
                }
                                                                                                                
                
    $add_month 0;
                while (
    smoothdate ($first['year'], $first['month'] + 1$first['day']) <= $target)
                {
                    
    $add_month++;
                    
    $first['month']++;
                    
                    if (
    $first['month'] > 12)
                    {
                        
    $first['year']++;
                        
    $first['month'] = 1;
                    }
                }
                                                                                                                                                                                
                
    $add_day 0;
                while (
    smoothdate ($first['year'], $first['month'], $first['day'] + 1) <= $target)
                {
                    if ((
    $first['year'] % 100 == 0) && ($first['year'] % 400 == 0))
                    {
                        
    $month_lengths[1] = 29;
                    }
                    else
                    {
                        if (
    $first['year'] % == 0)
                        {
                            
    $month_lengths[1] = 29;
                        }
                    }
                    
                    
    $add_day++;
                    
    $first['day']++;
                    if (
    $first['day'] > $month_lengths[$first['month'] - 1])
                    {
                        
    $first['month']++;
                        
    $first['day'] = 1;
                        
                        if (
    $first['month'] > 12)
                        {
                            
    $first['month'] = 1;
                        }
                    }
                    
                }
                                                                                                                                                                                                                                                            
                
    $retval = array ('years' => $add_year'months' => $add_month'days' => $add_day);
            }
        }
                                                                                                                                                                                                                                                                                    
        return 
    $retval;
    }


    /*
        This code is merely an example of use of the function
    */

    print '<pre>';
    $begin = array ('year' => 2001'month' => 3'day' => 14);
    $end = array ('year' => 2004'month' => 3'day' => 14);

    $foo date_difference ($begin$end);
    if (
    $foo !== FALSE)
    {
        
    print_r ($foo);
    }
    else
    {
        print 
    'FALSE';
    }

    ?>

  8. #8
    SitePoint Enthusiast bokz_42's Avatar
    Join Date
    Sep 2006
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply guys...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •