Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Math with dates

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
tdm
Smarty Rookie


Joined: 01 May 2003
Posts: 18

PostPosted: Mon May 05, 2003 8:41 am    Post subject: Math with dates Reply with quote

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
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Mon May 05, 2003 12:27 pm    Post subject: Reply with quote

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
View user's profile Send private message
tdm
Smarty Rookie


Joined: 01 May 2003
Posts: 18

PostPosted: Mon May 05, 2003 1:53 pm    Post subject: Reply with quote

Awsome, that works perfectly Very Happy
I transformed it into another Smarty Function, my third already today Confused

Is it a good way (without much performance lost) to create Smarty functions of most things which are often used on my site?

Thanks! Laughing
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP