Page 1 of 1
Date issue
Posted: Wed Jul 14, 2004 5:28 pm
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
Posted: Wed Jul 14, 2004 6:21 pm
by feyd
time() - unix_timestamp(`b-day`) = how many seconds old.
seconds / 31557600 = how many years old (roughly)
Posted: Mon Jul 19, 2004 8:41 pm
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
Posted: Mon Jul 19, 2004 10:10 pm
by feyd
it takes the forms used internally.. so DATE, TIME, DATETIME, TIMESTAMP and the others..
Posted: Tue Jul 20, 2004 12:49 am
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;
?>
Posted: Tue Jul 20, 2004 2:02 am
by feyd
I was talking about using the SQL (specifically, MySQL) DATE/TIME functions:
http://dev.mysql.com/doc/mysql/en/Date_ ... tions.html
Posted: Tue Jul 20, 2004 2:09 am
by lolpix
You are right. That would be much more direct.
Posted: Tue Aug 03, 2004 7:59 pm
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.
Posted: Tue Aug 03, 2004 8:24 pm
by feyd
Code: Select all
SELECT UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(`user_birthday`) `seconds_old` FROM `users`