Joomla 2.5 displays wrong datetime

怎甘沉沦 提交于 2019-12-30 05:14:24

问题


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?


回答1:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!