Convert timezones?
Posted: Mon Apr 26, 2010 12:51 am
There is nothing I hate more(maybe form validation scripts) than dealing with dates, times, and timezones. Just terrible.
I have a method that is called whenever I want to output a readable date(all of my dates and times are stored as unix timestamps). In the method, I want it to convert the time based on the user's timezone.
Shouldn't this convert the time from the server time from date('r') to the timezone I get from the user's settings? The $timestamp passed to this method is generated with time(). The timezones for the users are stored like 'America/Indianapolis'.
I have a method that is called whenever I want to output a readable date(all of my dates and times are stored as unix timestamps). In the method, I want it to convert the time based on the user's timezone.
Code: Select all
public function formatDate($timestamp, $time = false) {
$dateformat = Model_CurrentUser::getInstance()->dateFormat;
$timeformat = Model_CurrentUser::getInstance()->timeFormat;
$timezone = Model_CurrentUser::getInstance()->timezone;
//$result = '';
$date = new DateTime(date('r', $timestamp), new DateTimeZone($timezone));
// .............
// Then at the end of the method
$result = $date->format($dateformat);
return $result;