July
7

I have a short code snippet to share, if you want to do simple date calculations using the Smarty template engine. As a very short preamble, I've used Smarty off-and-on for eight years, and it's great. I find its simplicity and power outweigh bloated frameworks on a regular basis.

If you need the date for tomorrow, you can do it in two ways.

The first way is to create the variable in the "code behind", assign it to the template and then you can use it:

In PHP:

$smarty->assign('tomorrow', strtotime('+1 day'));

In Smarty template (or use a date format of your choice):

{$tomorrow|date_format:"%Y-%m-%d"}

The second way is done in the template alone. This is useful if you did not, or did not want to, assign a variable in the code behind. It is also useful if you wanted to use multiple dates (tomorrow, the next day, next week, etc.), or you could even use it in a loop:

In Smarty template:

{math equation=x+y x=$smarty.now y=86400 assign=tomorrow}
{$tomorrow|date_format:"%Y-%m-%d"}

This uses the UNIX timestamp value of $smarty.now, and adds 86,400 seconds to it. Unfortunately you cannot easily add exactly one month - or one year - using this method because each month or year could have a different number of seconds in it.

In future, I plan to say a little more about Smarty and its virtues.

2