View previous topic :: View next topic |
Author |
Message |
wincen Smarty n00b
Joined: 18 Mar 2009 Posts: 3
|
Posted: Wed Mar 18, 2009 1:20 pm Post subject: specify time zone with date_format? |
|
|
Is it possible to specify the time zone or offset with date_format?
I have a unix time stamp that I'm passing to smarty. I am able to determine the client's time zone, and I want to display the date and time in the client's time zone.
My preference is to have smarty change the timestamp, not have my php code loop through a bunch of objects and change all their timestamps to date times.
I have been searching but have not found anything other than specifying %Z which only shows the server's time zone. |
|
Back to top |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
Posted: Wed Mar 18, 2009 2:39 pm Post subject: |
|
|
You can change timezone in PHP with something like this
Code: | ini_set('date.timezone', 'America/Los_Angeles'); |
or
Code: | date_default_timezone_set('America/Los_Angeles'); |
Timezones list
http://www.php.net/manual/en/timezones.php |
|
Back to top |
|
wincen Smarty n00b
Joined: 18 Mar 2009 Posts: 3
|
Posted: Thu Mar 19, 2009 1:02 am Post subject: |
|
|
yes, that is how i'm setting my default time zone but the reason i'd prefer not to do this is because it changes the times on my logger. that is why i'd prefer to do it just on smarty. is this possible? |
|
Back to top |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
Posted: Thu Mar 19, 2009 5:56 am Post subject: |
|
|
No, not with one command.
Smarty is "only" a template engine ... |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Mar 19, 2009 2:51 pm Post subject: |
|
|
If you want to print a unix timestamp in a specific timezone, the simplest way is to momentarily set the new timezone in php, print the time, and set it back. You can do this with a smarty plugin. It won't bother your PHP dates elsewhere. |
|
Back to top |
|
wincen Smarty n00b
Joined: 18 Mar 2009 Posts: 3
|
Posted: Sat Mar 21, 2009 11:39 am Post subject: |
|
|
thanks for all the replies. setting the timezone in a plugin and then setting it back seems to be the right solution. |
|
Back to top |
|
Jebus905 Smarty n00b
Joined: 17 Jan 2018 Posts: 1
|
Posted: Wed Jan 17, 2018 5:46 pm Post subject: |
|
|
I know this is a 9 year old post, but I'm going to reply to it anyways because I was looking for a solution and this thread was one of the top Google results.
This plug-in is a quick and easy solution to the problem, save it as "modifier.changetimefromUTC.php" in your smarty plugins folder:
Code: | <?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File: modifier.changetimefromUTC.php
* Type: modifier
* Name: changetimefromUTC
* Purpose: Converts a UTC timestamp to the given timezone
* -------------------------------------------------------------
*/
function smarty_modifier_changetimefromUTC($time, $timezone) {
$changetime = new DateTime("@$time", new DateTimeZone('UTC'));
$changetime->setTimezone(new DateTimeZone($timezone));
return $changetime->format('Y-m-d H:i:s');
}
|
This will change any TIMESTAMP into a formatted date for the given time zone.
example:
Code: | {$timestamp|changetimefromUTC:'EST'} |
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Jan 18, 2018 1:45 pm Post subject: |
|
|
I'd supply format as a third parameter, if I were you.
Or, even better, second. And use strftime(). And unixtime by default. And test if initial value is already a DateTime descendant. |
|
Back to top |
|
|