Welcome Guest, Not a member yet? Register   Sign In
Using timespan
#1

[eluser]quindo[/eluser]
I'm using timespan in a test.
I use it to display my age using this command:
Code:
timespan(mktime(5,0,0,3,3,1973),$now);
With the meaning of giving me the difference between the current date and my birthday (3-March-1973).

However, today (24-Feb-2008, 14:32) it gives this output:
35 Years, 1 Day, 9 Hours, 32 Minutes

Somewhere something went wrong.
Anybody any idea what I'm doing wrong?
#2

[eluser]Seppo[/eluser]
The function assumes all years has 365 days and each month as 30 days... so in a long period you will have problems with the leap years
#3

[eluser]mdavis1982[/eluser]
Hi...

You might want to check out http://uk.php.net/manual/en/ref.datetime.php which has some examples for calculating the difference between two dates in the comments.

It seems like it would fit the bill completely!

Thanks...

Matt
#4

[eluser]quindo[/eluser]
Thank you for that information.
I used the code there to replace the timespan code in the codeigniter date helper:
Code:
function timespan ($t1=1, $t2 = '', $precision = 6, $abbr = false)
    {
        if (! function_exists('JP_date_trans'))
    {
          function JP_date_trans($datumnaam)
          {
            $CI =& get_instance();

            $CI->lang->load('date');
            $ret="";
            
            switch($datumnaam)
            {
                case 'year':
                    $ret = $CI->lang->line('date_year');
                    break;
                case 'years':
                    $ret = $CI->lang->line('date_years');
                    break;
                case 'month':
                    $ret = $CI->lang->line('date_month');
                    break;
                case 'months':
                    $ret = $CI->lang->line('date_months');
                    break;
                case 'week':
                    $ret = $CI->lang->line('date_week');
                    break;
                case 'weeks':
                    $ret = $CI->lang->line('date_weeks');
                    break;
                case 'day':
                    $ret = $CI->lang->line('date_day');
                    break;
                case 'days':
                    $ret = $CI->lang->line('date_days');
                    break;
                case 'hour':
                    $ret = $CI->lang->line('date_hour');
                    break;
                case 'hours':
                    $ret = $CI->lang->line('date_hours');
                    break;
                case 'minute':
                    $ret = $CI->lang->line('date_minute');
                    break;
                case 'minutes':
                    $ret = $CI->lang->line('date_minutes');
                    break;
                case 'second':
                    $ret = $CI->lang->line('date_second');
                    break;
                case 'seconds':
                    $ret = $CI->lang->line('date_seconds');
                    break;
            }
            return $ret;
          }
       }
    
        

        if ( ! is_numeric($t1))

            $t1 = 1;

    

        if ( ! is_numeric($t2))
            $t2 = time();

                
        if (preg_match('/\D/', $t1) && ($t1 = strtotime($t1)) === false)
            return false;

        if (preg_match('/\D/', $t2) && ($t2 = strtotime($t2)) === false)
            return false;

        if ($t1 > $t2)
            list($t1, $t2) = array($t2, $t1);

        $diffs = array(
            'year' => 0, 'month' => 0, 'day' => 0,
            'hour' => 0, 'minute' => 0, 'second' => 0,
            );

        $abbrs = array(
            'year' => 'yr', 'month' => 'mth', 'day' => 'day',
            'hour' => 'hr', 'minute' => 'min', 'second' => 'sec'
            );

        foreach (array_keys($diffs) as $interval)
        {
            while ($t2 >= ($t3 = strtotime("+1 ${interval}", $t1)))
            {
                $t1 = $t3;
                ++$diffs[$interval];
            }
        }


        $stack = array();
        foreach ($diffs as $interval => $num)
        {
            $stack[] = array($num, JP_date_trans($interval. ($num != 1 ? 's' : '') ) );
        }

        $ret = array();
        while (count($ret) < $precision && ($item = array_shift($stack)) !== null)
        {
            if ($item[0] > 0)
                $ret[] = "{$item[0]} {$item[1]}";
        }
    
        return implode(', ', $ret);
    }
#5

[eluser]mdavis1982[/eluser]
No Problem...

I'm glad it helped!

Cheers...

Matt




Theme © iAndrew 2016 - Forum software by © MyBB