Jump to content

Setting a users age


graham23s

Recommended Posts

Hi Guys,

 

when i get the users birthyear from mysql i do a simple bit of math to get the users age:

 

<?php
// work out the users age //
$year = date('Y');
$users_age = $year - $users_birth_year;
?>

 

$users_birth_year contains 1979 for example so

 

2008 - 1979 = 28

 

but im trying to think of the best way to get exactly the users age, so when i turn 29 in june how to update the age then?

 

thanks guys

 

Graham

Link to comment
Share on other sites

Hi Mate,

 

ah i see, the way i have set it i have:

 

<?php
print("<td class='form_style' align='left'><label><b>Birthdate:</b></label></td><td class='form_style' align='left'>\n");
print("<select name='birth_month'>\n");
foreach($birth_month as $key => $birth_month_value)
{
  print("<option value='$key'>$birth_month_value</option>\n");
}
print("</select>\n");

print("<select name='birth_day'>\n");
for($i = 1; $i <= 31; $i++)
{
  print("<option value='$i'>$i</option>\n");
}
print("</select>\n");

print("<select name='birth_year'>\n");
for($year = 1930; $year <= 2008; $year++)
{
  print("<option value='$year'>$year</option>\n");
}
print("</select>\n");
print("</td>\n");
?>

 

i have been individually storing the month,day,year into mysql seperately, instead of all in the 1 column

 

whats the best way to do all 3 in the 1 column? just grab all 3 values then stitch them together before the mysql insertion:

 

$dob = $bmonth . "-" . $bday . "-" .  $byear;

 

or is there a better way?

 

thanks mate

 

Graham

Link to comment
Share on other sites

heres a function i created that calculates the age

of my users.

 

$birth = mm/dd/yyyy

ex: $birth = 01/04/1967

 

heres the function:

<?php
	function age($birth){
		$blown = explode("/", $birth);

		$year = date("Y");
		$month = date("m");
		$day = date("d");

		$n_year = $year - $blown[2];
		$n_month = $month - $blown[1];
		$n_day = $day - $blown[0];

		if($n_month < 0){
			$age = $n_year - 1;
		} else{
			if($n_day < 0){
				$age = $n_year - 1;
			} else{
				$age = $n_year;
			}
		}
		return $age;
	}

$birth = "01/02/1967";

age($birth);
?>

Link to comment
Share on other sites

Check the php manual on DateTime functions. Particularly the date_create() function which will create a DateTime object for you, you can then use for date/time arithmetics or for SQL insertion.

 

A typical example would probably be:

 

$dateobj = datecreate('08 23 1969');

 

You can test $dateobj for 'false' in case the data is invalid.

 

EDIT:

Alternatively you can also use:

 

$timestamp = strtotime('08 23 1969');

 

It has the same effect and probably the best option for SQL insertion. However probably not cross-compatible with other databases not being served on UNIX.

 

EDIT2: both functions use the British form of month, day, year. Corrected my own examples.

Link to comment
Share on other sites

Whatever else you do, you can avoid lots of future pain by storing dates in an unequivocal format. Using the ISO yyyy-mm-dd for stored date format allows you flexibility and simplicity that will never exist if you choose some quaint local format for 'date'.  Note that storing date in a rational format does not preclude displaying it in whatever weird form floats your boat.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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