Date issue
Moderator: General Moderators
-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
Date issue
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
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
-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
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;
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
I was talking about using the SQL (specifically, MySQL) DATE/TIME functions: http://dev.mysql.com/doc/mysql/en/Date_ ... tions.html
-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
SELECT UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(`user_birthday`) `seconds_old` FROM `users`