PHP Smarty date conversion specifiers
I’m not a big fan of Smarty Templates in PHP. This is only the second project in a short space of time I’ve been required to use it due to existing code already implementing Smarty. The project is on a hectic deadline and we’ve been held up by unforeseen circumstances, but I know its no excuse! I assumed (yes… I know what they say about assumptions…) that Smarty’s date specifiers would be the same as PHP’s. *NOTE* That is not the case. PHP got its own style of specifiers while Smarty uses C style specifiers. It is also exactly the same as MySQL’s date specifiers.
Let say you get a datetime value out of your MySQL database and want to print the date as 5 Oct 2007 17:05. The MySQL format is 2007-10-05 17:05:00.
Now in normal PHP it would be:
echo date('j M Y H:i', strtotime($mysqlDate)); //$mysqlDate is the date from the database
Here is the same thing in Smarty:
{$mysqlDate|date_format:"%e %b %Y %H:%M"}
In Mysql it would look like this:
SELECT DATE_FORMAT(`date_column`, '%e %b %Y %H:%M') AS newDate FROM the_table WHERE ...;
I guess one could argue that Smarty should adapt to PHP. If you consider that PHP is developed in C, would it not make more sense for PHP to use C style specifiers?
Here is a list of specifiers for Smarty:
http://www.phpfreaks.com/smarty_manual/page/language.modifier.date.format.html
Here is PHP’s list:
http://www.php.net/manual/en/function.date.php
Here is for MySQL:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
2 Comments to "PHP Smarty date conversion specifiers"
Spit it out!
PHP, Programming Stii
Recent Posts
- Afrigator vs Regator
- Don’t pirate music/movies! You might be forced to use Windows if you do…
- Pike > Python?
- Using Twhirl for FriendFeed
- Being anti-social SUCKS!
- Example using XFN and Optimus to find friends on Afrigator
- Optimus, the ultimate Microformats parser
- Please support UnitedForAfrica.co.za
- Afrigator Beta2.0 - Lets amplify the Xenophobia issue
- What can we as bloggers do about the Xenophobia crisis?
My Posse
- Jayx’s bloggy
- Gogo’s blog
- Go2 South Africa
- Stumble Upon
- Dave Duarte
- Wikipedia
- zlythern
- Max Kaizen
- Tresblue
- Mike Stopforth
- RafiQ
- Muti.co.za
- Employmint
- Danette’s Bloggy!
- Thinking Machine
- White African
- kiefpiet.co.za
- Skuff’s World
- Goozeberry
- Crossloop blog
- Crossloop
- Aquila Online
- Charl van Niekerk
- Derek Allard
- Code Igniter
- Carls
- Justin Hartman
- blik.co.za
- Stefano Sessa
- Uno de Waal
- Amplitude!
- bLaugh
- Tyler Reed
- Chris Rawlinson
- Stormhoek!
- 3am
- Mike Solomon
- Mobile Q and A
- Eric Edelstein
- Marc Forrest
- Imel Rautenbach
- Absolutewillie
- Vincent Maher
- Colin Daniels
- Groogle!
- Chilibean
- Paul Jacobson
- Ayelet
- Python Guru Neil
- Rails Guru Nic
- Beverley Merriman
- Miguel
- Nic Harrywhatshisname
- Chris iMod
- Geekrebel!
- Steven McD
- Belinda sweetheart!
- Henre Rossouw
- JPGeek
- Foxinni
- Adii
- Charl Norman
- Bandwidthblog
- Jason Bagley
- Simon Botes
- Auric Silverwing
- Mark Forrester
- Saul Kropman
- Fred Roed
- Sass Schultz
Filed in
- Afrigator (26)
- ajax (9)
- API (2)
- Apple stuff (10)
- Blogging (24)
- browsers (5)
- Business (27)
- Code Igniter (8)
- firefox (8)
- flock (14)
- Funnies (72)
- GeekDinner! (18)
- General and sometimes Rants (47)
- Go2SA (2)
- ideas 2.0 (14)
- javascript (12)
- Kick-ass Tools (30)
- Linux (5)
- Marketing (25)
- moo.ajax (4)
- mootools (6)
- Open Source (10)
- Programming (33)
- C# (1)
- PHP (13)
- Python (9)
- Ruby (on Rails) (9)
- RSS (5)
- Semantic Web (32)
- Social Web (57)
- Software Development (15)
- South Africa (33)
- Tagging (6)
- Techie stuff (22)
- Tshirts (2)
- Tutorials (42)
- Blogging (17)
- Flocking (6)
- muti.co.za (13)
- Web 2.0 (73)
- web development (20)
Past Stuff
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006















I recently finished off an enormous project which had been built using Smarty. To make this even more fun, the codebase is deployed under Windows using PortableWebAp. Technically this shouldn’t be a problem, but for the small fact that Smarty checks the compiled template’s mtime against the actual template’s mtime to determine whether or not it should be recompiled…
Ah yes, we’re going to get an accurate mtime from Windows file systems, right ? *Sighs*. So we’re going to have the ‘check_compile’ and ‘force_compile’ directives set to true all the time ? I wanted to kick a choice individual’s head in when I solved that problem.
This problem does make sense because the plugins/modifier.date_format.php uses strftime() rather date(), but it is still quite vexing (and I’m glad I’ve never stumbled into it). If this one is really grinding you down, then you might have better luck by adding a new modifier.stii_date.php file into the plugins directory and have it use date() instead of strftime().
Is there anything worse than completing a system, which has already been deeply coded with a framework? My previous job was a massive system, which was built on a custom CMS system, consisting of 35+ tables in the database, advanced permissions system, and I was to complete the system - it took 2months just to start getting the hang of all the custom functions, etc.
I swore that I would never work on a system like this again, what an ACHE!