|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
tdm Smarty Rookie
Joined: 01 May 2003 Posts: 18
|
Posted: Mon May 05, 2003 8:41 am Post subject: Math with dates |
|
|
I want to calculate the age (in years) of a user within a template, I tried it with
Code: | {math equation="now - x" x=$results[uid].user_birthdate now=$tmpl.now} |
but then I get an error:
Code: | Warning: Smarty error: math: parameter x: is not numeric |
How could I get this working?
Thanks |
|
Back to top |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Mon May 05, 2003 12:27 pm Post subject: |
|
|
hmm... this requires some effort... you better do that in PHP:
Code: | function getAge($dateOfBirth) {
if (!ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $dateOfBirth, $regs))
return false;
$age = date("Y") - $regs[1];
if ($regs[2] > date("m"))
$age--;
elseif ($regs[2] == date("m"))
if ($regs[3] > date("d"))
$age--;
return $age;
} |
from user comments on php.net/date |
|
Back to top |
|
tdm Smarty Rookie
Joined: 01 May 2003 Posts: 18
|
Posted: Mon May 05, 2003 1:53 pm Post subject: |
|
|
Awsome, that works perfectly
I transformed it into another Smarty Function, my third already today
Is it a good way (without much performance lost) to create Smarty functions of most things which are often used on my site?
Thanks! |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|