PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Aug 20, 2017 9:30 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 44 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject:
PostPosted: Wed May 23, 2007 10:23 am 
Offline
Site Administrator
User avatar

Joined: Tue Sep 09, 2003 6:04 pm
Posts: 14293
Location: Fremont, CA, USA
What is the rest of your code? There could be things making that age different.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 23, 2007 12:13 pm 
Offline
Forum Regular
User avatar

Joined: Mon Nov 14, 2005 5:21 pm
Posts: 878
olrac wrote:
Hello,

I have a question for this code is this the right bdate code?
because i tried these code and isn't working i mean it does but my age is 22. on that code it show my age is 23.

$dat=explode("-",$rs->fields[bdate]);
$age=(date("Y")- $dat[0]);

Thank you.


You need these lines as well.

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

        {

                $age -= 1;

        }


See it would probably subtract one year from the value giving your correct data. You need the whole routine I posted.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 2:53 am 
Offline
DevNet Resident

Joined: Tue Apr 23, 2002 10:02 am
Posts: 1199
Location: Brisbane
I am working on a project with a friend of mine who suggested i use this bit of maths...and he doesn't even dabble in php much.

Syntax: [ Download ] [ Hide ]
//removed - didnt work as one would hope from simple things.

_________________
pixel glue | domain monkey


Last edited by qads on Mon Jun 18, 2007 3:46 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 3:00 am 
Offline
Forum Regular
User avatar

Joined: Mon Nov 14, 2005 5:21 pm
Posts: 878
Hate to tell you but it won't be acurate for every day. :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 3:44 am 
Offline
DevNet Resident

Joined: Tue Apr 23, 2002 10:02 am
Posts: 1199
Location: Brisbane
heh, thanks for the heads up.

I'll remove the code in the post.

_________________
pixel glue | domain monkey


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 9:55 am 
Offline
DevNet Resident

Joined: Tue Apr 23, 2002 10:02 am
Posts: 1199
Location: Brisbane
Right, this should be good for any month, returns your exact age down to a decimal point.

Syntax: [ Download ] [ Hide ]
function age($month, $year)

{

return (date("Y")+(date("m")/12)) - ($year+($month/12));

}

_________________
pixel glue | domain monkey


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 3:18 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
Exact age... relative to the month. It's no more accurate than that. :P


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 3:27 pm 
Offline
Site Administrator
User avatar

Joined: Sun May 19, 2002 10:24 pm
Posts: 6883
Just glancing at that, I would say it most likely doesn't take leap years into account, and therefore is not 100% accurate.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 4:24 pm 
Offline
DevNet Master
User avatar

Joined: Sun Jan 21, 2007 12:06 am
Posts: 4135
:lol:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 01, 2007 3:20 pm 
Offline
Site Administrator
User avatar

Joined: Sun May 19, 2002 10:24 pm
Posts: 6883
heh, just had a need to rewrite it..

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;
}


set it and forget it..


Top
 Profile  
 
PostPosted: Mon Sep 20, 2010 1:46 am 
Offline
Forum Newbie

Joined: Mon Sep 20, 2010 1:20 am
Posts: 12
take it and use it immediately


Top
 Profile  
 
PostPosted: Wed Oct 20, 2010 4:32 pm 
Offline
Forum Contributor

Joined: Sat Jan 03, 2009 4:27 pm
Posts: 148
The OP's code fails if the the birth date's day is greater than the current date's day even if the month of the former is less than the latter (which, of course, gives the wrong result). I'd suggest the following modification:

Syntax: [ Download ] [ Hide ]

    function getAge($date) {

      list($year, $month, $day) = explode('-', $date);

      $age = date('Y') - $year;

      if (($month > date('m')) || ($month == date('m') && $day > date('d'))) {
        return $age - 1;
      }

      return $age;

    }
 


Top
 Profile  
 
PostPosted: Wed Oct 20, 2010 4:48 pm 
Offline
Site Administrator
User avatar

Joined: Sun May 19, 2002 10:24 pm
Posts: 6883
The code in the first post did have a bug, I'll update it. The code posted a few posts up works fine.

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

get_age(2000, 1, 1);
get_age(2000, 10, 1);
get_age(2000, 1, 30);
get_age(2000, 10, 19);
get_age(2000, 10, 20);
get_age(2000, 10, 21);
get_age(2000, 12, 1);
get_age(2000, 12, 24);


Syntax: [ Download ] [ Hide ]
Age is 10 if born on 1/1/2000
Age is 10 if born on 10/1/2000
Age is 10 if born on 1/30/2000
Age is 10 if born on 10/19/2000
Age is 10 if born on 10/20/2000
Age is 9 if born on 2000/21/2000
Age is 9 if born on 2000/1/2000
Age is 9 if born on 2000/24/2000

_________________
Image


Top
 Profile  
 
PostPosted: Fri Apr 15, 2011 1:17 am 
Offline
Forum Newbie
User avatar

Joined: Tue Apr 12, 2011 11:47 am
Posts: 16
Here is also one method that may help you out.

please apply and check :

(round((DATEDIFF(NOW(), 1980-03-13)/365))


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

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