Jump to content

[SOLVED] date of birth as age?


marksie1988

Recommended Posts

i have the flowing table

 

CREATE TABLE `band` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(50) NOT NULL,
  `pos` varchar(50) NOT NULL,
  `dob` date NOT NULL default '0001-01-01',
  `bio` text NOT NULL,
  `equip` text NOT NULL,
  `songw` text NOT NULL,
  `inf` text NOT NULL,  
  `goals` text NOT NULL,
  `myspace` carchar(200) NOT NULL,
  `image` text NOT NULL,
  PRIMARY KEY  (`id`)
)

 

and i also have a page that displays the information but i want to display the dob (date of birth) column as the age of a person how do i do this? currently all i can get it to do is show todays date or the date from the table but i cant get it to show an age.

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/63737-solved-date-of-birth-as-age/
Share on other sites

Here is the basic concept:

 

<?
$dateOfBirth = "1980-02-01";

// Split the date of birth into an array
$dateOfBirth = explode('-', $dateOfBirth);

// create a timestamp of the users date of birth
$dobTimestamp = mktime(0,0,0,$dateOfBirth['1'], $dateOfBirth['2'], $dateOfBirth['0']);

// Subtract date of birth timestamp from the current time
$ageInSeconds = $dobTimestamp - time();

// you now have the number of seconds the person has been alive for.
// Simply do the math to change it to days, years etc.
?>

sorted it out now thanks for the help :)

 

here is the code finished

 

$dateOfBirth = $row['dob'];

// Split the date of birth into an array
$dateOfBirth = explode('-', $dateOfBirth);

// create a timestamp of the users date of birth
$dobTimestamp = mktime(0,0,0,$dateOfBirth['1'], $dateOfBirth['2'], $dateOfBirth['0']);

// Subtract date of birth timestamp from the current time
$ageinseconds = time() - $dobTimestamp; 
$days = $ageinseconds / 86400;
$years = floor($days / 365);
$days = $days % 365;

 

then you just echo $years for the number of years and $days for the number of days

Ohh Andy, always with the raining!! :)

 

<?php

$dateOfBirth = $row['dob'];

// Split the date of birth into an array
$dateOfBirth = explode('-', $dateOfBirth);

// create a timestamp of the users date of birth
$dobTimestamp = mktime(0,0,0,$dateOfBirth['1'], $dateOfBirth['2'], $dateOfBirth['0']);

// Subtract date of birth timestamp from the current time
$ageinseconds = time() - $dobTimestamp;
$days = $ageinseconds / 86400;
$years = floor($days / 365);
$days = $days % 365;

$leapDays = floor($years / 4);

$days = $days + $leapDays;

?>

 

is this correct? Makes sense to me but I've been working to much today and my mind is cooked!

 

 

I just realized this isn't correct. The day addition doesn't funnel itself down through the years/months etc so the days have to be added before any other calcuation is made.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.