WebmasterSite.net: PHP scripts to enable your creativity
WSN Links PHP Directory Software
PHP Scripts Webmaster Links Support Forums


custom function calls
I'm a posting fool today

Version:
printPrint


custom function calls
knotworking
Experienced

Usergroup: Customer
Joined: Mar 31, 2004
Total Topics: 20
Total Posts: 83
Posted 05/02/05 - 08:31 PM:
quote post
#1
I went over the manual and got a fair understanding of how you can create functions in the different classes that can then be called in your templates. Here's my dilemma:

I have a birth_date field in my integrated database in unix format ('0000-00-00'). I would like to display the user age on the view_profile. I tried to use this function:

function calculateage() {
$amember = new member('id', 'birth_date', $id);
$cur_year=date("Y");
$cur_month=date("m");
$cur_day=date("d");

$dob_year=substr($birth_date, 0, 4);
$dob_month=substr($birth_date, 5, 2);
$dob_day=substr($birth_date, 8, 2);

if($cur_month>$dob_month || ($dob_month==$cur_month && $cur_day>$dob_day) )
return $cur_year-$dob_year-1;
else
return $cur_year-$dob_year;
}

This is not working (though I've been able to get it to display various wrong data when tweaking). The main issue is I do not understand how to call a member's 'birth_date' field into the function (using the appropriate sql). I assigned{MEMBERBIRTH_DATE} into the view profile and the '0000-00-00' value was displayed as expected. Can you elaborate on how I can call these values into a custom function that I could then use.

Thanks!
Thanks
Paul
Administrator
Avatar

Usergroup: Administrator
Joined: Dec 21, 2001
Location: Northern California
Total Topics: 55
Total Posts: 5703
Posted 05/03/05 - 12:45 PM:
quote post
#2
in unix format ('0000-00-00')

That's not a unix timestamp, it's mysql format.

Fields are called as member variables of the class. The member birthdate in WSN Forum is stored as a mysql date as well and the function to show the age is this, in classes/member.php:


function age()
{
if ($this->birthyear == '0000') return '0';
$t = adjusttotimezone(time());
$thisyear = strftime("%Y", $t);
$thismonth = strftime("%m", $t);
$thisday = strftime("%d", $t);
$age = $thisyear - $this->birthyear;
if ($this->birthmonth > $thismonth) $age -= 1;
if ($this->birthmonth == $thismonth && $this->birthday > $thisday) $age -= 1;
return $age;
}


For you in WSN Gallery, though, you'll have to parse up your date into the day month and year first for that to work (WSN Forum does that in the constructor). So, this should work for you:


function age()
{
$bits = explode('-', $this->birth_date);
$this->birthyear = $bits[0];
$this->birthmonth = $bits[1];
$this->birthday = $bits[2];
if ($this->birthyear == '0000') return '0';
$t = adjusttotimezone(time());
$thisyear = strftime("%Y", $t);
$thismonth = strftime("%m", $t);
$thisday = strftime("%d", $t);
$age = $thisyear - $this->birthyear;
if ($this->birthmonth > $thismonth) $age -= 1;
if ($this->birthmonth == $thismonth && $this->birthday > $thisday) $age -= 1;
return $age;
}


Just use {MEMBERAGE} to display.

Note that birthdates/ages will be standard in Gallery in a few months.

Edit: I'm not sure if the adjusttotimezone function applies in Gallery. If that causes an error, just remove it and use the plain time().

Edited by Paul on 05/03/05 - 12:54 PM
knotworking
Experienced

Usergroup: Customer
Joined: Mar 31, 2004
Total Topics: 20
Total Posts: 83
Posted 05/03/05 - 02:12 PM:
quote post
#3
Well that works swimmingly!

Funny, I've been working at it for the last three hours and was just about to give up.

I never would have come up with your function, THANKS! (heck, I can't even recognize timestamps!)
Search thread for
Download thread as


You don't have permission to post.

Please login or register.

   
 
© 2008 Paul Knierim. All rights reserved.