How to get the correct time in my localhost?

2,361 views
Skip to first unread message

PGS

unread,
Aug 17, 2016, 4:19:43 AM8/17/16
to Joomla! General Development
        I 'm trying to get the correct time.
           
        I followed these instructions I found in the internet:
        1) In httpd.conf (\xampp\apache\conf\httpd.conf) , I added the following line:

        SetEnv TZ Europe/Athens

        2) Edit php.ini (\xampp\php\php.ini) date.timezone value in [Date] section:

        date.timezone = "Europe/Athens"

        3) In my.ini (\xampp\mysql\bin\my.ini) added:

        default-time-zone = "Europe/Athens"

        Restarted Apache and MySQL.
       
      
       
        $db = JFactory::getDbo();      
        $nowDate
= $db->quote($date->toSql());


       
       
        Output:
        '2016-08-17 10:00:00' while the actual date is '2016-08-17 11:00:00'. (Looks like winter time?

PGS

unread,
Aug 17, 2016, 4:21:55 AM8/17/16
to Joomla! General Development
I forgot to mention that joomla 's timezone is 'Athens'.

Tuan Pham Ngoc

unread,
Aug 17, 2016, 5:11:33 AM8/17/16
to Joomla! General Development
You don't have to change timezone settings on apache or php.ini. Use the code below will give you the current time in the timezone you select in Global Configuration of your site:

$date = JFactory::getDate('now', JFactory::getConfig()->get('offset'));
echo $date->toSql(true);

Please note in the $date->toSql() method, you need to pass true as a value for $local parameter (it is false by default), otherwise, the system will return the time in GMT

PGS

unread,
Aug 17, 2016, 7:31:19 AM8/17/16
to Joomla! General Development
Tuan your code gives me 2 hours ahead:
16:00 while the actual time is 14:00.

Settings in httpd.conf, php.ini, my.ini:
in httpd.conf I commented out the SetEnv: #SetEnv TZ Europe/Athens
in php.ini I have: date.timezone=Europe/Athens
in my.ini: default-time-zone = "Europe/Athens"
Joomla->Global configuration-> Server-> Server time zone: "Athens"    


Tuan Pham Ngoc

unread,
Aug 17, 2016, 8:07:27 AM8/17/16
to Joomla! General Development
Sorry, I have no clue why it doesn't work on your computer. I just tried to set Server Time Zone in Global Configuration of Joomla to Athens, then run the code and it displays

2016-08-17 15:05:45

That's the same with the time displayed here http://www.timeanddate.com/worldclock/greece/athens, so I don't know what's wrong with the code. 

PGS

unread,
Aug 17, 2016, 10:28:44 AM8/17/16
to Joomla! General Development
It doesn't read correctly the UTC:
 
       $date = new JDate('now', 'UTC');        
        echo
'UTC';
        echo
"<br>";
        echo $date
;
        echo
"<br>";

Output is 16:26 while the actual UTC is 14:26!

Ove Eriksson

unread,
Aug 18, 2016, 3:24:05 AM8/18/16
to Joomla! General Development
It's recommended to use UTC timezone internally, convert to users timezone and date/timeformat for outputs and convert userinput back to UTC.


 JDate('now', 'UTC')

works for me. I*m also on UTC+1. Local time is summertime!
UTC: 2016-08-18 06:30:31, Local user time 2016-08-18 08:30:31

The Joomla methods helps with the conversions.

The instructions you followed might not be that useful for Joomla.

I don't know what the httpde.conf setting is influencing. Probably your setting for MySql is the problem. I do not want to test, as I*m not sure what happens if I add a timezone to my.ini.
I think, the date related filetypes are stored as UTC and only shown as defined in the settings when using the db-tools.
If not, you're in trouble, if you have important data in the database.

(I do have a setting in php.ini: date.timezone = Europe/Xyz, Where Xyz can be Athens or any other supported city  )

Please test and let us know. 

Ove Eriksson

unread,
Aug 18, 2016, 3:33:30 AM8/18/16
to Joomla! General Development
Edit: You don't have to add UTC to JDate(*now*), It's default.
Reply all
Reply to author
Forward
0 new messages