You are here: Re: [PHP] date() problem « PHP « IT news, forums, messages
Re: [PHP] date() problem

Posted by "Richard Lynch" on 07/06/05 23:59

On Wed, July 6, 2005 12:07 pm, Ryan A said:
> I'm confused, this should give me the age as 17 instead of 16...but it
> does
> not...any ideas why?
>
> <?php print date("Y:m:d");
> $age="1988-07-06";
>
> $day1=strtotime($age);
> $day2 = strtotime(date("Y-m-d"));
> $dif_s = ($day2-$day1);
> $dif_d = ($dif_s/60/60/24);
> $age = floor(($dif_d/365.24));
>
> echo $age;
> ?>

365.24 is an appoximation.

Sooner or later, it's gonna bit you in the butt.

If you want somebody's age accurately, you're probably going to have to do
it the hard way.

Something like this might work:

<?php
$DOB = "1988-07-06";
$now = date('Y-m-d');
list($by, $bm, $bd) = explode('-', $DOB);
list($ny, $nm, $nd) = explode('-', $now);
$age = $ny - $by;
if ($age){
if ($bm < $nm){
// do nothing, they were born before this month
// so subtracting the years is correct
}
elseif ($nm < $bm){
$age--; //They were born in a later month, so not quite a year yet
}
else{
//They were born this month. Count the days.
if ($bd < $nd){
//Do nothing. They were born before this date.
}
elseif ($nd < $bd){
$age--; //They were born later this month, so not quite a year yet
}
else{
//It's their birthday! Go ahead and count it as a year,
//unless you want to futz with the hour of their birth...
}
}
}
if (!$age){
//Do whatever you want with children less than 1 year old...
//return it as "X months" or just call it "infant" or pretend they're 1
//or whatever.
//Maybe even just use $age (which is 0) and call it done.
}
?>

--
Like Music?
http://l-i-e.com/artists.htm

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация