Separate Date & Time Fields¶
Sometimes it may be desired to have date and time fields separated. For example, in the News Module, you may want to alter the template file to have the article date at the top of the news article, and the time at the bottom of the article. In order to accomplish this, you need to take the default date string and convert it into a valid date time object. Then, if desired, format it according to your tastes. The first step is to find the line in your template (.tpl) file:
1 <?php echo $this->date; ?>
and convert the $this->date into a date/time object (originally, it is a string). To accomplish this, you wrap the $this->date in the date_create() function like this:
1 date_create($this->date)
The next step is to apply the date formatting function (if desired):
1 date_format(date_create($this->date), "F j, Y")
Your entire line will look like this:
1 <?php echo date_format(date_create($this->date), "F j, Y"); ?>
And the resulting output to the page will be Month Day, Year (e.g. June 22, 2007). The "F j, Y" parameter denotes the date/time format that we are using, according to the PHP Reference for date formats.
You can take the PHP line and apply it to any .tpl file that references $this->date. (such as news_full.tpl)
Please note that date_create() and other DateTime related functions are included by default only in PHP versions equal and greater than 5.2. In PHP 5.1.2 this functionality is marked to be experimental and has to be enabled at compile time.
--- Tutorial created by fbliss