Reputation: 1600
In a Zend Framework project I am using Zend_Date
to save user entry time. But the use case is ome thing that when I am inserting data from a time zone,it should be converted to the user who is from another time zone.
As an example I am inserting data from "Asia/Dhaka"
time zone, another user should watch it by the conversion in his own timezone.
I have gone through previous similar threads, unfortunately couldn't able to grab that fully.
Thanks in advance.
Upvotes: 2
Views: 2440
Reputation: 1600
I wrote a simple method to solve the issue:
public function timeZoneFormat($time)
{
$currentTimeZone = date_default_timezone_get();
$timestamp = strtotime($time);
$customTime= new Zend_Date();
$customTime->setTimestamp($timestamp);
$customTime->setTimezone("$currentTimeZone");
return $customTime;
}
Upvotes: 0
Reputation: 9148
Although Zend_Date has a nice feature set for working with date/time, it is known to be a lot slower than PHP's native Date object and functions.
For your question though, as per the Zend documentation: internally dates are always stored as timestamps and in GMT. So the timezone means how much hours should be substracted or added to get the actual global time for the own timezone and region.
So, looking at the api there are a few methods that could help you to easily calculate time zone difference when displaying or even better, switch the timezone of your date object. If you are storing date in database, it would probably be easier to store the timestamp returned by $date->getUnixTimestamp()
instead of the date object's output. Either that or you will also need the original timezone to calculate the offset.
A slightly modified example for your needs, taken from the documentation in example #6:
date_default_timezone_set('Asia/Dhaka');
// create a date object
$date = new Zend_Date('06.15.2011 00:00:00', Zend_Date::ISO_8601, 'de');
print $date;
$otherdate = clone $date;
$otherdate->setTimezone('America/Montreal');
print $otherdate;
Upvotes: 1