PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Feb 27, 2017 1:46 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 44 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: Mon Jun 19, 2006 12:24 pm 
Offline
Site Administrator
User avatar

Joined: Sun May 19, 2002 10:24 pm
Posts: 6883
Send date of birth in format YYYY-MM-DD

Syntax: [ Download ] [ Hide ]
function get_age($year, $month, $day) {
  if (date('m') - (int)$month >= 0) {
      if (date("d") - (int)$day >= 0 || date('m') > $month) {
          return date('Y') - (int)($year);
      }
  }
  return date('Y') - (int)($year) - 1;
}

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 19, 2006 2:20 pm 
Offline
Site Admin
User avatar

Joined: Tue Dec 23, 2003 3:10 am
Posts: 11470
Location: Toronto
you love stroring your dates in different formats don't you ;)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 19, 2006 2:48 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5975
Location: Odessa, Ukraine
My thoughts exactly =).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 19, 2006 7:28 pm 
Offline
DevNet Master

Joined: Tue Jan 20, 2004 12:11 am
Posts: 4897
Location: Leuven, Belgium
With MySQL the query would be like:

Syntax: [ Download ] [ Hide ]

SELECT YEAR(CURRENT_DATE()) - YEAR(birthday) - (RIGHT(CURRENT_DATE(),5)<RIGHT(birthday,5)) AS age

FROM foo;

 


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 19, 2006 8:40 pm 
Offline
Forum Regular
User avatar

Joined: Mon Nov 14, 2005 5:21 pm
Posts: 878
Similar way.. :)

Syntax: [ Download ] [ Hide ]
function GetAge($DOB) {

        $birth = explode("-", $DOB);



        $age = date("Y") - $birth[0];



        if(($birth[1] > date("m")) || ($birth[1] == date("m") && date("d") < $birth[2]))

        {

                $age -= 1;

        }

        return $age;

}


Man, I had to edit this this 3-4 times before I got it as small as I wanted. ;)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 5:59 am 
Offline
DevNet Master
User avatar

Joined: Tue Nov 02, 2004 6:43 am
Posts: 2704
Location: Ireland
The mighty timestamp would beat you all...:).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 1:21 pm 
Offline
Site Administrator
User avatar

Joined: Sun May 19, 2002 10:24 pm
Posts: 6883
How does that work?

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 2:03 pm 
Offline
DevNet Resident
User avatar

Joined: Fri Apr 07, 2006 5:13 am
Posts: 1640
Location: Israel
Maugrim_The_Reaper wrote:
The mighty timestamp would beat you all...:).

Actually, it wouldn't. This could be a much better function:
Syntax: [ Download ] [ Hide ]
<?php

        function get_age($age)

        {

                $diff = time() - strtotime($age);





                return (int) ($diff / (3600 * 24 * 365));

        }

?>


The above function is more simple and can accept almost any date format. One problem though... A big problem... What about people who were born before 1.1.1970?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 6:04 pm 
Offline
Tranquility In Moderation
User avatar

Joined: Sun Feb 06, 2005 8:18 pm
Posts: 5001
Location: Indiana
Oren wrote:
One problem though... A big problem... What about people who were born before 1.1.1970?


What about the extra remainders of the 365 days? leap years? timezones? :-P

_________________
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 6:38 pm 
Offline
Forum Contributor

Joined: Sun Dec 26, 2004 10:04 pm
Posts: 110
This part doesn't make sense to me

Syntax: [ Download ] [ Hide ]
if ($DayDifference < 0 || $MonthDifference < 0) {
   $YearDifference--;
}


So if I'm born on May 21, 2005 and today is June 20, 2006 I should be 1 years old... but according to this I would be 0.

Please put intval()'s around your $Year, $Month, $Day, and date() values.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 1:02 am 
Offline
Forum Contributor
User avatar

Joined: Sun May 07, 2006 5:19 am
Posts: 101
Quote:
One problem though... A big problem... What about people who were born before 1.1.1970?


Not really a problem, in PHP timestamps can be a negative integer to represent before 1970. Therefore, even if your string was something like "12 May 1944", then strtotime() would convert it to a negative timestamp and calculate the difference properly and you would get "62" years as the correct result.

As for timezones , they hardly matter when you are counting in terms of years.

To avoid the problem with leap years you can use 31556926 as the number of seconds in a year, and then calculate the value as a float (floor() it to make it an integer).

A modfiied version of Oren's function would be:

Syntax: [ Download ] [ Hide ]
function yearsSince($date){

                $date = is_string($date) ? strtotime($date) : $date;           

                $secondsSince = time() - $date;

                $secondsInAYear = 31556926;

                $yearsSince = floor($secondsSince / $secondsInAYear);

                return $yearsSince;

}


Which accepts dates as strings or unix timestamps and returns a more precise value.


Last edited by Verminox on Wed Jun 21, 2006 1:14 am, edited 3 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 1:07 am 
Offline
Forum Contributor

Joined: Sun Dec 26, 2004 10:04 pm
Posts: 110
Verminox wrote:
Not really a problem, in PHP timestamps can be a negative integer to represent before 1970.

Not in PHP 4


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 1:11 am 
Offline
Forum Contributor
User avatar

Joined: Sun May 07, 2006 5:19 am
Posts: 101
Ahh I did not know that. Well so I guess PHP4 users will only have to deal with those younger than the age of 36 :P


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 2:03 am 
Offline
Forum Regular
User avatar

Joined: Mon Nov 14, 2005 5:21 pm
Posts: 878
Well, the one I posted works just fine. :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 2:59 am 
Offline
DevNet Resident
User avatar

Joined: Fri Apr 07, 2006 5:13 am
Posts: 1640
Location: Israel
scottayy wrote:
What about the extra remainders of the 365 days? leap years?

I know, just didn't bother to mention that since the function isn't good anyway :wink:
Ohh and... I could be wrong, but time() returns seconds since 1.1.1970 so there shouldn't be any problems with leap years.

scottayy wrote:
timezones? :P

Verminox wrote:
As for timezones , they hardly matter when you are counting in terms of years.


Verminox wrote:
Ahh I did not know that. Well so I guess PHP4 users will only have to deal with those younger than the age of 36 :P

I run PHP 5.1.4 and when I entered a date before 1.1.1970 the function returned 36, so I guess the negative timestamps don't work after all.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 44 posts ]  Go to page 1, 2, 3  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group