calculate difference in dates

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alexislalas
Forum Commoner
Posts: 44
Joined: Sun Feb 19, 2006 10:09 pm

calculate difference in dates

Post by alexislalas »

i want to get the age of the people. i get it in the form as the date of birth 0000/00/00.


when i do a report, how do i get the age.


thanks...
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

look at this old post:

viewtopic.php?t=39916&start=0

it's not exactly what you're looking for, but will get you started on the right foot
csingsaas
Forum Newbie
Posts: 22
Joined: Thu Jul 06, 2006 9:34 pm
Location: Edina, MN

Post by csingsaas »

You should use something similar to the following to break apart the date into month, day and year elements.


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

$d = explode(" ",$today);
$date1 = $d[0];

$date_x = explode("-",$date1);
$year = $date_x[0];
$month = $date_x[1];
$day = $date_x[2];


So if you had the date 2004-06-24, the variables would be set to:

$year = 2004
$month = 06
$day = 24

From here you can perform basic math equations on the variables. Hope this helps.


Regards,
Cody Singsaas
IE Internet Solutions
http://www.ieinternetsolutions.com
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Please, when posting PHP, wrap it in

Code: Select all

bbcode tags.

[quote="csingsaas"]You should use something similar to the following to break apart the date into month, day and year elements. 

[syntax="php"]<?php
$today = date("Y-m-d");

$d = explode(" ",$today);
$date1 = $d[0];

$date_x = explode("-",$date1);
$year = $date_x[0];
$month = $date_x[1];
$day = $date_x[2];
?>
So if you had the date 2004-06-24, the variables would be set to:

Code: Select all

<?php
$year = 2004
$month = 06
$day = 24
?>
From here you can perform basic math equations on the variables. Hope this helps.


Regards,
Cody Singsaas
IE Internet Solutions
http://www.ieinternetsolutions.com[/quote][/syntax]
Post Reply