Jump to content

Recommended Posts

Ok, fairly noob at php so please bear with me..

 

I'm doin the old calculate persons age from database routine. Had a good search around and found out about Unix timestamps. Been looking those up and trying to figure out how to use them properly.

 

My dates are of the d-m-Y format.. I.E.. today is 11-02-2007

I did it this way so i can use date("d-m-Y") to grab todays date elsewhere in the site and it's all the same format.

 

Problem is that I can't quite grasp timestamps.

I tired something like this..

 

$dobts = strtotime($dateofbirth);
$todaysdate = date("d-m-Y");
$todaysdatets = strtotime($todaysdate);

$agets = $todaysdatets - $dobts;

$age = date("d-m-Y", $agets);

 

Didn't work of course but it thought that was the point of these timestamps?

 

Can anyone explain it better please?

ok tried a bit more..

 

Had another look round and found a post or two that explained a little better.. but it's still not working.

 

here's the code now..

 

        $todaysdate = date("d-m-Y");

$diff = abs(strtotime($todaysdate) - strtotime($dateofbirth));
$day = 60 * 60 * 24; // seconds in a day
$agedays = $diff / $day; 
$age = floor($agedays/365);

 

but with $todaysdate = 22-05-1982

it chucks out 10... and should be 25.

I think maybe the strtotime doesn't like my date inputs?

 

Having a little read of the immense php manual tells me it takes a US date input.. so is that the problem?

if so it means re-writing alot of code or figuring a way to trick strtotime()..

 

So if this is the problem does anyone know a way of getting 22-05-1982 to 05-22-1982 before i use strtotime() on it?

 

any help appreciated.

Save the data as a timestamp usually

 

//get timestamp in seconds and store in database
$timestamp = time();
//load timestamp from database then
//code to print date according to timestamp's value
$date = date("M-d-Y",$timestamp);

Still having trouble with this..

Lots of trouble..  ???

 

Anyways.. so i have three list boxes for users to enter their date of birth.

Days, months and years.

 

I can change this to a timestamp now using strtotime. I think i'm doing it right..

$dateofbirth  = $dobyear . "-" . $dobmonth . "-" . $dobday;
$dobts = strtotime($dateofbirth);

 

now i store $dobts in my database..

 

Now the problem comes trying to display the persons age.

 

I grab the current timestamp-

$currentts = time();

 

then I'm guessing you subtract the date of birth from the current time.. leaving us with the amount of seconds since this person was born.

 

$agesecs = $currentts - $dobts;

 

then some trickery to make it into years... which im also kinda guessing at..

$age = $agesecs *60 *60 *24 *365;

 

but with the date of birth 22nd May 1982 this gives me the amazing out put of - 2.5112491353072E+16

 

so yeah... wrong..

 

please help, I don't have much hair left :P

 

 

 

 

You need to divide by seconds in a year, not multiply.

 

try

<?php
$dobts = mktime(0,0,0,5,22,1982);

$age = date('Y') - date('Y', $dobts);
if (date('md') < date('md', $dobts)) $age--;

echo $age;
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.