Calculate Date of Birth?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
adilmarwat2004
Forum Commoner
Posts: 44
Joined: Fri Sep 04, 2009 11:28 pm

Calculate Date of Birth?

Post by adilmarwat2004 »

I have database in mysql and has a table name "act" contains four fields as

id(int), date_birth(date), date_close(date), age(date)
1 03-09-1987 20-03-2010
2 05-06-1989 20-03-2010

I want that set the query for age field like that it takes date from field 'date_close' and subtract it from 'date_birth' and display in age field.

How it is possible, please help?

Adil
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Calculate Date of Birth?

Post by Christopher »

Convert them both to timestamps (strtotime() ?), subtract, and then convert back to year, months and days (from seconds).
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Calculate Date of Birth?

Post by josh »

http://dev.mysql.com/doc/refman/5.1/en/ ... tions.html
DATE_SUB() or subtract... the second column's timestamp divided by the number of seconds in a day could be a possible subtractor.

DATE_SUB(date,INTERVAL expr unit)

so start_date would be the date paramater
the unit would be days
and the timestamp could be used to calculate how many days you are subtracting..

just one possible way.
Post Reply