I have Created this code in a custom component that I made:
$date = date('m/d/Y h:i:s a', time())."<br>";
echo 'Current date and time is: ' . $date;
$date = JFactory::getDate();
echo 'Current date and time is: ' . $date->toFormat() ."<br>";
The first code displays the datetime correctly, but the second one displays the time +3 hours
I have checked the configuration.php file and the public $offset = 'Europe/Athens'; and is correct. I am also changing the settings from the system configuration menu but nothing seems to be fixing the JFactory::getDate() to display the correct time. What am I missing?
For your second parameter in the JFactory::getdate()
- I think you should be specifying the time zone in a second parameter so something like JFactory::getDate($time=now, $tzOffset)
$date = JFactory::getDate($input='now', 'UTC');
// Set the correct time zone based on the server configuration.
$config = JFactory::getConfig();
$date->setOffset($config->getValue('config.offset'));
//Print out Date
echo $date->toFormat();
On a side note it may be easier to use JHtml::date() in the component as this involves less lines and is more 'Joomla native'. See the API page on this here. Then use a code like :
echo JHtml::date($input = 'now', 'm/d/Y h:i:s a', false);
Where $input
= now specifies to use the time 'now'. The second parameter is your format for the date and the third parameter means the time setting is set the server time. Rather than the users selected time.
来源:https://stackoverflow.com/questions/12637322/joomla-2-5-displays-wrong-datetime