Firefox PHP

Hack: Birthday/Display Age

Posted by Ryan 
Hack: Birthday/Display Age
August 10, 2009 11:13PM
This is a hack to allow users to enter their birthday in the Control Center. They have the option of displaying their age in their profile, and if they choose to, the age is calculated and displayed on the profile page.

If someone wants to turn this into a module, be my guest. I don't have the expertise to do it myself. Brian Moon helped with most of this, so thanks Brian!

1. Create the following custom fields: month, day, year, show_age

2. In cc_usersettings.tpl, add the following:
Quote

<dt>Birthday:&nbsp;</dt>
<dd><select name="month">
<option value="">--------</option>
<?php
for ($x=1;$x<=12;$x++) {

echo "<option value=\"$x\"";
if($PHORUM["DATA"]["PROFILE"]["month"]==$x) echo " selected=\"selected\"";
echo ">".date("F", strtotime("2009-$x-1"))."</option>";
}
?>
</select>&nbsp;
<select name="day">
<option value="">--</option>
<?php
for ($x=1;$x<=31;$x++) {

echo "<option";
if($PHORUM["DATA"]["PROFILE"]["day"]==$x) echo " selected=\"selected\"";
echo ">".date("j", strtotime("2009-1-$x"))."</option>";
}
?>
</select>&nbsp;
<select name="year">
<option value="">----</option>
<?php
for($x=date("Y");$x>=date("Y")-99;$x--) {
echo "<option";
if($PHORUM["DATA"]["PROFILE"]["year"]==$x) echo " selected=\"selected\"";
echo ">$x</option>";
}
?>
</select>&nbsp;
Display age in profile? <input type="radio" name="show_age" value="1"{IF PROFILE->show_age "1" OR NOT PROFILE->show_age} checked="checked"{/IF} /> Yes <input type="radio" name="show_age" value="0"{IF PROFILE->show_age "0"} checked="checked"{/IF} /> No</dd>

3. In profile.tpl, add:
Quote

{IF PROFILE->month AND PROFILE->day AND PROFILE->year AND NOT PROFILE->show_age "0"}
<tr>
<td><b>Age:</b></td>
<td><?php
function birthday ($birthday) {
list($year,$month,$day) = explode("-",$birthday);
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
if ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}
echo birthday("{$PHORUM['DATA']['PROFILE']['year']}-{$PHORUM['DATA']['PROFILE']['month']}-{$PHORUM['DATA']['PROFILE']['day']}");
?>
</td>
</tr>
{/IF}

-------------------------------------------

"Everything we see or seem, is but a dream within a dream." -Edgar Allan Poe
Re: Hack: Birthday/Display Age
August 11, 2009 02:55AM
I admire both your and Brian's stamina on getting this hack going. Reading the IRC logs what quite the job this morning ;-)


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: Hack: Birthday/Display Age
September 03, 2009 02:24PM
Thanks! This is awesome and works flawlessly.

I just wanted to add that I assigned a value of 2 to each field except for the 'year', to which I assigned 4 (in case anybody was wondering, this worked for me).

- Bob
Sorry, only registered users may post in this forum.

Click here to login