Handling date profile fields

Last modified: August 27, 2009 - 00:30

Adding dates to your custom profile.

This has only been tested with 4.7.0 and 5.3

This module adds a birthday into the custom profile, but you can easily customize it to add any date, such as date of death for a obituary (for a famous actor's profile maybe). You must have made a template.php already. This code should be added to your user_profile.tlp.php file.

<div class="fields"><b>Birthday:</b> <?php print $user->profile_birthday['month']."/".$user->profile_birthday['day']."/".$user->profile_birthday['year'];?>

Note that you will have to customize the profile_birthday to whatever your settings are in admin/settings/profile. To change the date layout, just move the month, day, and year around, like so:

<div class="fields"><b>Birthday:</b> <?php print $user->profile_birthday['year']."/".$user->profile_birthday['month']."/".$user->profile_birthday['day'];?>

Convert date array to date variable

Here's a simple function that will convert the profile date array to a date variable. Maybe a little clunky but you get the point:

<?php
function convert_profile_date($profile_date_ar)  {

      
// takes a profile date array and converts it to a date variable.

   
$output = mktime(0, 0, 0, $profile_date_ar['month'], $profile_date_ar['day'], $profile_date_ar['year']);
    return
$output;

}
?>

Then you can pass the results to the php date function and format any way you want:

<?php
$converted_date
= convert_profile_date($user->profile_birthday);
print
date('F j, Y', $converted_date);
?>

D6 error with convert_profile_date()

arilikeairy - June 4, 2009 - 22:15

Received this error when I tried to put that last snippet into Drupal 6.8:

Call to undefined function convert_profile_date()

Did you enable a module with the invoked function?

victorkane - June 9, 2009 - 16:55

It doesn't come with Drupal, you have to add it in.

I have successfully used the snippet in D6 (latest release 6.12).

Victor Kane
http://awebfactory.com.ar

Same here. It's a very handy

GetLives - July 11, 2009 - 03:45

Same here. It's a very handy function. I started using it because you can't just use the "format_date" code.

Please consider joining my site: http://www.getlives.com
Thank you. -GetLives

Great Script

ianhoar - October 22, 2009 - 03:43

Love the script, I did a small change though so I can use one function in template files.

<?php
function convert_profile_date($profile_date_ar, $formate)  {
   
// takes a profile date array and converts it to a date variable.
   
$output = mktime(0, 0, 0, $profile_date_ar['month'], $profile_date_ar['day'], $profile_date_ar['year']);
   
$output = date($formate, $output);
    return
$output;
}
?>

This lets you pass your date format to the convert_profile_date function. Now you can use for example:

<?php
    convert_profile_date
($account->profile_date_of_birth, 'F j, Y');
?>

http://www.ianhoar.com
Ian Hoar - Passion for Technology - Geeking Out

 
 

Drupal is a registered trademark of Dries Buytaert.