Displaying date of birth & age

Discussion forum for MOD Writers regarding MOD Development.
Locked
DJ Skitzo
Registered User
Posts: 596
Joined: Sun Aug 12, 2007 7:25 pm

Displaying date of birth & age

Post by DJ Skitzo »

Hello,

Could anyone please tell me how I would go about to display a users age and date of birth on a custom page.

Thanks.
DJ Skitzo
Registered User
Posts: 596
Joined: Sun Aug 12, 2007 7:25 pm

Re: Displaying date of birth & age

Post by DJ Skitzo »

Anyone?
A.I. BOT
Registered User
Posts: 1848
Joined: Thu Apr 17, 2003 11:43 pm
Location: Newfoundland, Canada
Contact:

Re: Displaying date of birth & age

Post by A.I. BOT »

You can try this...

Code: Select all

<?php
define ( 'IN_PHPBB', true );
$phpbb_root_path	= './';
$phpEx				= substr ( strrchr ( __FILE__, '.' ), 1 );
include ( $phpbb_root_path . 'common.' . $phpEx );

// Initilize the phpBB sessions.
$user->session_begin ( );
$auth->acl ( $user->data );
$user->setup ( 'mods/lang_rivals' );

$user_id = 2; // As an example.

// Get the user's birthdate.
$sql = "SELECT user_birthday FROM " . USERS_TABLE . " WHERE user_id = " . $user_id;
$result = $db->sql_query ( $sql );
$row = $db->sql_fetchrow ( $result );
$db->sql_freeresult ( $result );

// Get the user's DOB. Split it up into year, month and day.
$user_birthday	= strtotime ( $row[ 'user_birthday' ] );
$user_year		= date ( 'Y', $user_birthday );
$user_month		= date ( 'm', $user_birthday );
$user_day		= date ( 'j', $user_birthday );

// Get the difference between today's date and the user's birthdate.
$year_diff	= date ( 'Y' ) - $user_year;
$month_diff	= date ( 'm' ) - $user_month;
$day_diff	= date ( 'j' ) - $user_day;

if ( $month_diff == 0 )
{
	// It's the user's birthday MONTH. Check if it's their birth DAY.
	if ( $day_diff < 0 )
	{
		// It is not their birth DAY, yet.
		$year_diff--;
	}
}
else if ( $month_diff < 0 )
{
	// It is not their birth MONTH, yet.
	$year_diff--;
}

print 'Your age is: ' . $year_diff;
Hope it works for you...

Thanks.
DJ Skitzo
Registered User
Posts: 596
Joined: Sun Aug 12, 2007 7:25 pm

Re: Displaying date of birth & age

Post by DJ Skitzo »

Thanks I will give it a go soon.

I'm just a little confused from that code though, from the looks of things that code presumes the birth date has been stored as a UNIX time stamp, but my database has stored birthdays in a format of DD-MM-YYYY for some reason.
A.I. BOT
Registered User
Posts: 1848
Joined: Thu Apr 17, 2003 11:43 pm
Location: Newfoundland, Canada
Contact:

Re: Displaying date of birth & age

Post by A.I. BOT »

No, no, strtotime will CONVERT that DD-MM-YYYY to a UNIX timestamp.
DJ Skitzo
Registered User
Posts: 596
Joined: Sun Aug 12, 2007 7:25 pm

Re: Displaying date of birth & age

Post by DJ Skitzo »

Ok, thank you, going to test it as soon as I get some time. :D

Does anyone know how I could change this into a function so I could add it to my functions.php file and call it in my custom page using "get_user_age($birthdate)"?

I'v never built a function before so I'm not quite sure of how exactly you would get the ending result from it.
User avatar
3Di
Former Team Member
Posts: 16032
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Displaying date of birth & age

Post by 3Di »

Perhaps I'm missing something, isn't that already handled into index.php? Just a Q here.
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
Looking for a specific feature or alternative option? We will rock you! 🚀
A.I. BOT
Registered User
Posts: 1848
Joined: Thu Apr 17, 2003 11:43 pm
Location: Newfoundland, Canada
Contact:

Re: Displaying date of birth & age

Post by A.I. BOT »

Here is a tutorial (many more if you search through google).

Code: Select all

/**
 * Gets a user's age based on their birthdate.
 *
 * @param integer $user_id
 * @return integer
*/
function get_user_age ( $user_id )
{
	global	$db;

	// Get the user's birthdate.
	$sql	= "SELECT user_birthday FROM " . USERS_TABLE . " WHERE user_id = " . $user_id;
	$result	= $db->sql_query ( $sql );
	$row	= $db->sql_fetchrow ( $result );
	$db->sql_freeresult ( $result );

	// Get the user's DOB. Split it up into year, month and day.
	$user_birthday	= strtotime ( $row[ 'user_birthday' ] );
	$user_year		= date ( 'Y', $user_birthday );
	$user_month		= date ( 'm', $user_birthday );
	$user_day		= date ( 'j', $user_birthday );

	// Get the difference between today's date and the user's birthdate.
	$year_diff	= date ( 'Y' ) - $user_year;
	$month_diff	= date ( 'm' ) - $user_month;
	$day_diff	= date ( 'j' ) - $user_day;

	if ( $month_diff == 0 )
	{
	   // It's the user's birthday MONTH. Check if it's their birth DAY.
	   if ( $day_diff < 0 )
	   {
		  // It is not their birth DAY, yet.
		  $year_diff--;
	   }
	}
	else if ( $month_diff < 0 )
	{
	   // It is not their birth MONTH, yet.
	   $year_diff--;
	}

	// The user's age.
	return	$year_diff;
}
Put that into your functions.php file and call by "get_user_age ( $user_id );" example "$user_age = get_user_age ( $row[ 'user_id' ] );"

$user_age will now contain the user's age (given they do have a birthdate set in the UCP).

Thanks :)
User avatar
3Di
Former Team Member
Posts: 16032
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Displaying date of birth & age

Post by 3Di »

3Di wrote:Perhaps I'm missing something, isn't that already handled into index.php? Just a Q here.
Hmmm.. from index.php:

Code: Select all

// Generate birthday list if required ...
$birthday_list = '';
if ($config['load_birthdays'] && $config['allow_birthdays'])
{
	$now = getdate(time() + $user->timezone + $user->dst - date('Z'));
	$sql = 'SELECT user_id, username, user_colour, user_birthday
		FROM ' . USERS_TABLE . "
		WHERE user_birthday LIKE '" . $db->sql_escape(sprintf('%2d-%2d-', $now['mday'], $now['mon'])) . "%'
			AND user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')';
	$result = $db->sql_query($sql);

	while ($row = $db->sql_fetchrow($result))
	{
		$birthday_list .= (($birthday_list != '') ? ', ' : '') . get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']);

		if ($age = (int) substr($row['user_birthday'], -4))
		{
			$birthday_list .= ' (' . ($now['year'] - $age) . ')';
		}
	}
	$db->sql_freeresult($result);
}
feeel free to arrange it to fit your needs.
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
Looking for a specific feature or alternative option? We will rock you! 🚀
DJ Skitzo
Registered User
Posts: 596
Joined: Sun Aug 12, 2007 7:25 pm

Re: Displaying date of birth & age

Post by DJ Skitzo »

A.I. BOT wrote:Here is a tutorial (many more if you search through google).

Code: Select all

/**
 * Gets a user's age based on their birthdate.
 *
 * @param integer $user_id
 * @return integer
*/
function get_user_age ( $user_id )
{
	global	$db;

	// Get the user's birthdate.
	$sql	= "SELECT user_birthday FROM " . USERS_TABLE . " WHERE user_id = " . $user_id;
	$result	= $db->sql_query ( $sql );
	$row	= $db->sql_fetchrow ( $result );
	$db->sql_freeresult ( $result );

	// Get the user's DOB. Split it up into year, month and day.
	$user_birthday	= strtotime ( $row[ 'user_birthday' ] );
	$user_year		= date ( 'Y', $user_birthday );
	$user_month		= date ( 'm', $user_birthday );
	$user_day		= date ( 'j', $user_birthday );

	// Get the difference between today's date and the user's birthdate.
	$year_diff	= date ( 'Y' ) - $user_year;
	$month_diff	= date ( 'm' ) - $user_month;
	$day_diff	= date ( 'j' ) - $user_day;

	if ( $month_diff == 0 )
	{
	   // It's the user's birthday MONTH. Check if it's their birth DAY.
	   if ( $day_diff < 0 )
	   {
		  // It is not their birth DAY, yet.
		  $year_diff--;
	   }
	}
	else if ( $month_diff < 0 )
	{
	   // It is not their birth MONTH, yet.
	   $year_diff--;
	}

	// The user's age.
	return	$year_diff;
}
Put that into your functions.php file and call by "get_user_age ( $user_id );" example "$user_age = get_user_age ( $row[ 'user_id' ] );"

$user_age will now contain the user's age (given they do have a birthdate set in the UCP).

Thanks :)
Thanks again :D
DJ Skitzo
Registered User
Posts: 596
Joined: Sun Aug 12, 2007 7:25 pm

Re: Displaying date of birth & age

Post by DJ Skitzo »

I finally got around to trying this function today, sadly it returns the wrong value.

My date of birth is 22nd June 1989, that function displayed my age as being 39 rather than 19.

Does anyone know what to change? I don't quite understand how it works.

Thanks :)
A.I. BOT
Registered User
Posts: 1848
Joined: Thu Apr 17, 2003 11:43 pm
Location: Newfoundland, Canada
Contact:

Re: Displaying date of birth & age

Post by A.I. BOT »

Ah sorry about that, I tried this one, it works for me :)

Fixed one:

Code: Select all

/**
* Gets a user's age based on their birthdate.
*
* @param integer $user_id
* @return integer
*/
function get_user_age ( $user_id )
{
   global   $db;

   // Get the user's birthdate.
   $sql   = "SELECT user_birthday FROM " . USERS_TABLE . " WHERE user_id = " . $user_id;
   $result   = $db->sql_query ( $sql );
   $row   = $db->sql_fetchrow ( $result );
   $db->sql_freeresult ( $result );

   // Get the user's DOB. Split it up into year, month and day.
   $user_birthday   = strtotime ( str_replace ( ' ', '', $row[ 'user_birthday' ] ) );
   $user_year      = date ( 'Y', $user_birthday );
   $user_month      = date ( 'm', $user_birthday );
   $user_day      = date ( 'j', $user_birthday );

   // Get the difference between today's date and the user's birthdate.
   $year_diff   = date ( 'Y' ) - $user_year;
   $month_diff   = date ( 'm' ) - $user_month;
   $day_diff   = date ( 'j' ) - $user_day;

   if ( $month_diff == 0 )
   {
      // It's the user's birthday MONTH. Check if it's their birth DAY.
      if ( $day_diff < 0 )
      {
        // It is not their birth DAY, yet.
        $year_diff--;
      }
   }
   else if ( $month_diff < 0 )
   {
      // It is not their birth MONTH, yet.
      $year_diff--;
   }

   // The user's age.
   return   $year_diff;
}
DJ Skitzo
Registered User
Posts: 596
Joined: Sun Aug 12, 2007 7:25 pm

Re: Displaying date of birth & age

Post by DJ Skitzo »

Thanks you, that seems to be working perfectly now. :D
Locked

Return to “[3.0.x] MOD Writers Discussion”