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

specify time zone with date_format?

 
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
wincen
Smarty n00b


Joined: 18 Mar 2009
Posts: 3

PostPosted: Wed Mar 18, 2009 1:20 pm    Post subject: specify time zone with date_format? Reply with quote

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


Joined: 07 Jun 2007
Posts: 321

PostPosted: Wed Mar 18, 2009 2:39 pm    Post subject: Reply with quote

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


Joined: 18 Mar 2009
Posts: 3

PostPosted: Thu Mar 19, 2009 1:02 am    Post subject: Reply with quote

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


Joined: 07 Jun 2007
Posts: 321

PostPosted: Thu Mar 19, 2009 5:56 am    Post subject: Reply with quote

No, not with one command.

Smarty is "only" a template engine ...
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Mar 19, 2009 2:51 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
wincen
Smarty n00b


Joined: 18 Mar 2009
Posts: 3

PostPosted: Sat Mar 21, 2009 11:39 am    Post subject: Reply with quote

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


Joined: 17 Jan 2018
Posts: 1

PostPosted: Wed Jan 17, 2018 5:46 pm    Post subject: Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Jan 18, 2018 1:45 pm    Post subject: Reply with quote

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
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