Page 1 of 1

Advanced Age Script.

Posted: Fri Mar 04, 2005 1:08 pm
by Supremacy
Hi guys, i have working for serval hours now, trying to create a age script.

and did it, i thought...

my problem is, that there is serval times every day where the script fails.

im born at nov 5th, 1980, at 7:15am.

this is what i got:

Code: Select all

function show_age() {
    setlocale (LC_ALL, 'da_DK');
    $str_bday = "5/11/1980 7:15";
    $ar = explode( ' ', $str_bday );
    $dmy = explode( '/', $arї0] );
    $hm = explode( ':', $arї1] );
    $ydiff = 2037 - date( 'Y' );
    $age = strtotime( "+$ydiff years" ) - mktime( $hmї0], $hmї1], 0, $dmyї1], $dmyї0], $dmyї2] + $ydiff );
    $y = strftime( "%Y", $age ) - 1970;
    $m = strftime( "%m", $age ) -1;
    $d = strftime( "%d", $age ) -1;        
    $t = strftime( "%k", $age) -1;
    $mins = strftime( "%M", $age);
    $mins = str_replace("0", "", $mins);
    
    if(!$mins) {
    $m = "0";
    }
    
    $months = "month";
    $days = "day";
    $hours = "hour";
    $minutes = "minut";
    
    if($m != "1") {
    $months = $months . "s";
    }
    
    if($d != "1") {
    $days = $days . "s";
    }
    
    if($t != "1") {
    $hours = $hours . "s";
    }
    
    if($mins != "1") {
    $minutes = $minutes . "es";
    }
    
    $age = strftime( "$y years, $m $months, $d $days, $t $hours and $mins $minutes", $age );
    
    return($age);
}
i've found out, that in the 7, 8, 9, 10th month, it puts +1 hour to the time, and in 3,4,5 (i think it was) it substracts an hour.

Andbody knows what's wrong, or even have a bulletproof age script??

Btw. I live in denmark, and GMC = +1.


feyd | please use

Code: Select all

tags while

Code: Select all

is offline[/color]

Posted: Fri Mar 04, 2005 1:15 pm
by feyd
daylight savings! :)

Posted: Fri Mar 04, 2005 1:17 pm
by Supremacy
i figured that out, but how do i solve my problem??

i have been about 7 hours on a danish forum, and it seems nobody there can solve it. :(

Posted: Fri Mar 04, 2005 1:22 pm
by feyd
pass false/0 for the last argument to mktime() ?

there's a time to seconds function in Code Snippets if you want to see another example.. sorta.

Posted: Fri Mar 04, 2005 1:26 pm
by Supremacy
I've tryied the 0 in mktime.

I've even tryed gmmktime... nothing helped.

there is various ways of getting the seconds from a timestamp.. it's not a problem.

i just want that <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> script to work as i want it. :P

Posted: Fri Mar 04, 2005 7:55 pm
by Supremacy
It seems after all, that we DO have som brilliant danes amongst us. :D :D

Code: Select all

<?php

    function calcAge( $str_bday )
    &#123;
        $ar = explode( ' ', $str_bday );
        $dmy = explode( '/', $ar&#1111;0] );
        $hm = explode( ':', $ar&#1111;1] );
    
        $yd = date( 'Y' ) - $dmy&#1111;2];
        $md = date( 'm' ) - $dmy&#1111;1];
        $dd = date( 'd' ) - $dmy&#1111;0];
        $hod = date( 'H' ) - $hm&#1111;0];
        $mid = date( 'i' ) - $hm&#1111;1];
        
        if ( $mid < 0 )
        &#123;
            // Negative minutes, adjust it and decrement hours
            $mid = 60 + $mid;
            $hod -= 1;
        &#125;
        if ( $hod < 0 )
        &#123;
            // Negative hours, adjust it and decrement days
            $hod = 24 + $hod;
            $dd -= 1;
        &#125;
        if ( $dd < 0 )
        &#123;
            // Negative days, what to do...
            if ( date( 'd', mktime( 0, 0, 0, date( 'm' ), -1, date( 'Y' ) ) ) < $dmy&#1111;0] )
            &#123;
                // The old date's day is past this month, so just set days to
                // the amount of days past in this month
                $dd = date( 'd' ) + 0;
            &#125;
            else
            &#123;
                // Calculate the day numbers of one month past with the old 
                // date's day, and the day number of today to get the days
                $before = mktime( 0, 0, 0, date( 'm' ) - 1, $dmy&#1111;0], date( 'Y' ) );
                $beforenum = date( 'z', $before ) + ( date( 'L', $before ) && date( 'm', $before ) > 2 ? 1 : 0 );
                $todaynum = date( 'z' ) + ( date( 'L' ) && date( 'm' ) > 2 ? 1 : 0 );
                $dd = $todaynum - $beforenum;
            &#125;
            // Decrement months
            $md -= 1;
        &#125;
        if ( $md < 0 )
        &#123;
            // Negative months, adjust it and decrement years
            $md = 12 + $md;
            $yd -= 1;
        &#125;
        // Return it
        $res = array(
                'year' => $yd
                ,'month' => $md
                ,'day' => $dd
                ,'hour' => $hod
                ,'min' => $mid
            );
        return $res;
    &#125;

    $str_bday = "5/11/1980 7:15";
    $age = calcAge( $str_bday );
    echo "$age&#1111;year] years $age&#1111;month] months $age&#1111;day] days $age&#1111;hour] hours $age&#1111;min] minutes";
    
?>
As far as i can see and test, this script works no matter what. :D