Web Design Forum  
 
Go Back   Web Design Forum > Web Software > Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-12-2008, 07:15 AM   #1 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 5
Default How to take out user's AGE in PHP?

This code i have made to take out age of the user(the date in the mktime is just for testing). But many problems are occurring like if the user was born on 2nd of a month today is 1st of a month then the date comes in minus (-) which makes it too confusing... After all, i m completely stuck & confused. Please tell how to take out users age.

PHP Code:
$td mktime(0,0,0,05,24,1991);
$td1 time();

$aa date("Y"$td);
$bb date("Y"$td1);
$age $bb $aa;

$cc date("m"$td);
$dd date("m"$td1);
$month $dd $cc;

$ee date("d"$td);
$ff date("d"$td1);
$days $ff $ee;

echo 
"$age years, $month months and $days days"
laksh_khamesra is offline   Reply With Quote
Old 11-03-2008, 01:18 AM   #2 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 2
Default

Your syntax appears correct. You just need to take out the negative sign or you need the absolute value. Luckily, php comes with tons of handy math functions at your disposal and unfortunately, you only need one. abs() is the function.

Code:
$days = abs($ff - $ee);
you might want to do this with all of them or you will have the same problem when the wrong time of the year rolls around.

Cheers,
Jonathan
durrban is offline   Reply With Quote
Old 11-21-2008, 06:05 PM   #3 (permalink)
New Member
 
Erni's Avatar
 
Join Date: Nov 2008
Posts: 9
Default

no, abs() will not show up the correct answer.
Calculations must be performed more carefuly and step by step.
Try to kick this:
Quote:
$d1 = array(2008,6,15);
$d2 = array(1998,5,21);

$days = $d1[2] - $d2[2];
if($days <= 0)
{
// we should add the quantity of days in the month
$days += date("d",mktime(0,0,0,$d1[1],$d1[2],$d1[0]));
$d1[1]--;
}
$months = $d1[1] - $d2[1];
if($months <= 0)
{
$months += 12;
$d1[0]--;
}
$age = $d1[0] - $d2[0];


echo "$age years, $months months and $days days";
__________________
WebAndPeople
trying to do my best. My Blog
Erni is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:38 AM.


Computer Forum - TechZine - Webpage Design

 
Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.