/    Sign up×
Community /Pin to ProfileBookmark

another DateDIff question

Hi

My code works but retuns an unformated value

age returns a value of 728559

Birth date was April 17 2001

how do I convert that to an age

[code=php]
<?
function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date – $start_date;
}

?>
[/code]

[code=php]
$age = dateDiff(“/”, $dob,date(‘m/d/y’));
[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@bokehJul 27.2006 — Your two major problem are: [list=1]
  • [*]not using a capital "Y" in your call to the date function (lower case "y" returns "06" which corresponds to 6AD)

  • [*]arguments 2 and 3 when calling your user defined function are in the wrong order

  • [/list]
    You also need to take leap years into consideration. Maybe something like this: [code=php]function age($dob /* format m/d/Y */, $delimiter ='/')
    {
    if(!function_exists('is_leap'))
    {
    function is_leap($year)
    {
    return (($year%4)?false:(($year%400)?true:false));
    }
    }
    list($month, $day, $year) = explode($delimiter, $dob);
    $days = gregoriantojd($m = date('m'), date('d'), $Y = date('Y'))
    - gregoriantojd($month, $day, $year);
    $age = 0;
    while($days >= ($remove = (is_leap((($m < 3))?--$Y:$Y--) ? 366 : 365)))
    {
    $days -= $remove;
    $age++;
    }
    return $age;
    }

    // test it
    echo age('04/17/2001');[/code]
    ×

    Success!

    Help @kproc spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...