5

I'm able to get the right "time" to display, but I am NOT able to show the correct Timezone in the formatted date that is output to the user.

I've been trying what I can find online and the docs, but can't seem to get that part of the output correct. Am I missing something fundamental, or is this a JDate bug?

My Output

Source Date:  2000-01-01 00:00:00 (stored in DB as UTC)
Expected Output:  1999-12-31 19:00 EST, using format "Y-m-d H:i T"

Joomla Timezone:  DateTimeZone Object
(
    [timezone_type] => 3
    [timezone] => UTC
)

User Timezone:  America/New_York
User DateTimeZone Object
(
    [timezone_type] => 3
    [timezone] => America/New_York
)

1. 2000-01-01 05:00 GMT
2. 2000-01-01 05:00 GMT    

My Code

$thedate = '2000-01-01 00:00:00';
echo '<pre>Source Date:  '.print_r($thedate,true).' (stored in DB as UTC)'."\n";
echo 'Expected Output:  2000-01-01 05:00 EST, using format "Y-m-d H:i T"</pre>';

// Joomla Timezone
$tz_joomla = new DateTimeZone(JFactory::getApplication()->get('offset'));
echo '<pre>Joomla Timezone:  '.print_r($tz_joomla,true).'</pre>';

// User Timezone
$user = JFactory::getUser();
$utzparm = $user->getParam('timezone', 'UTC');
$tz_user = new DateTimeZone($utzparm);
echo '<pre>User Timezone:  '.print_r($utzparm,true)."\n";
echo 'User '.print_r($tz_user,true).'</pre>';

// Output withOUT `->setTimezone`
$date = new JDate($thedate,$tz_user);
$output_date = $date->format('Y-m-d H:i T');
echo '<pre>1. '.print_r($output_date,true).'</pre>';

// Output WITH `->setTimezone`
$date = new JDate($thedate,$tz_user);
$date->setTimezone($tz_user);
$output_date = $date->format('Y-m-d H:i T');
echo '<pre>2. '.print_r($output_date,true).'</pre>';
3

The answer apparently lies in using ->calendar() instead of ->format() because that method has a second parameter to format locally. The below code gives me the timezone correctly in the output:

$date = new JDate($thedate);
$date->setTimezone($tz_user);
$output_date = $date->calendar('Y-m-d H:i T', true );
echo $output_date';

> 1999-12-31 19:00 EST
1
  • Please accept your answer.
    – mickmackusa
    Jan 16 '20 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.