Webmastersite.net
Register Log In

custom function calls
I'm a posting fool today

Comments on custom function calls

knotworking
Experienced

Usergroup: Customer
Joined: Mar 31, 2004

Total Topics: 20
Total Comments: 83
Posted May 02, 2005 - 10:31 PM:

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
developer

Usergroup: Administrator
Joined: Dec 20, 2001
Location: Diamond Springs, California

Total Topics: 61
Total Comments: 7868
Paul
Posted May 03, 2005 - 2:45 PM:

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().
knotworking
Experienced

Usergroup: Customer
Joined: Mar 31, 2004

Total Topics: 20
Total Comments: 83
Posted May 03, 2005 - 4:12 PM:

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
  • 0/5
  • 1
  • 2
  • 3
  • 4
  • 5



This thread is closed, so you cannot post a reply.