Date issue

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
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Date issue

Post by pinehead18 »

Ok, i manage age under my user based site, by the persons birthday but i noticed one big flaw. I manage the age by subtracting the current year by the year they were born. However, it does not take into affect what month they were born.

what is the best way to get the accurate date based off the year and month they were born. Am i looking for a math forumla or something?

Thank you
Anthony
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

time() - unix_timestamp(`b-day`) = how many seconds old.
seconds / 31557600 = how many years old (roughly)
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

Little confused. the unix time stamp b-day is that their actual bday and how would i format it in their?

Thank you
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it takes the forms used internally.. so DATE, TIME, DATETIME, TIMESTAMP and the others..
lolpix
Forum Commoner
Posts: 41
Joined: Sat Jul 17, 2004 2:20 am

Post by lolpix »

I think this is pretty much what feyd means — or at least one way of accomplishing the task along the same lines. You can obviously compact this into a couple lines of code, I was going for clarity.

Code: Select all

<?php

/*

NOTE: The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are  the dates that correspond to the minimum and maximum values for  a 32-bit signed integer.)  Additionally, not all platforms support negative timestamps, therefore  your date range may be limited to no earlier than the Unix epoch. This  means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems.

http://us2.php.net/manual/en/function.strtotime.php

*/



$day = (int) 10;
$month = 'January';
$year = (int) 1950;


$birthday = "$day $month $year";   // ex., 10 January 1950 




$birthday_as_timestamp = strtotime($birthday);   // Converts birthday to UNIX timestamp format.




$age_in_seconds = time() - $birthday_as_timestamp;   // Current timestamp minus age in UNIX timestamp format equals age in seconds.




$approximate_age = $age_in_seconds / 31557600;   // Age in years, leap year averaged in but not acurrately calculated. 60 * 60 * 24 * 365.25 = 31557600




$age = floor($approximate_age);   // Most likely the person's age in round years. Keep in mind that leap years are only estimated and time zone differences are not taken into account.




echo $age;



?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I was talking about using the SQL (specifically, MySQL) DATE/TIME functions: http://dev.mysql.com/doc/mysql/en/Date_ ... tions.html
lolpix
Forum Commoner
Posts: 41
Joined: Sat Jul 17, 2004 2:20 am

Post by lolpix »

You are right. That would be much more direct.
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

what is the benefit of using mysql time stamp? I've been trying to create a birthdate script based on the mysql time stamp. Yet i've been unsecsesful.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

SELECT UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(`user_birthday`) `seconds_old` FROM `users`
Post Reply