0

I am working on an application on my computer that is a time clock. When I click punch in, the time that is saved in the database if different then the time on my computer. I am using localhost to develop the application. I know it is probable a time zone problem. I found this question: is the time of computer and localhost same? , and followed the steps, with no success. I am using WampServer as a web server. I went through the php.ini file and made the changes to the date.timezone setting and nothing changed. I do have three versions of php installed and I made the changes to each version. I also found the Wamp use a symbolic link for the php.ini file that links to php4apache.ini, and made the changes in the linked file, still nothing worked.

I tried checking the time in php and mysql following the instruction here: Date/Time differences between MySql and PHP?. and found the mysql is set to SYSTEM, and php is set to UTC. This tells me I do not have PHP set correctly. So my questions are, is there another place in PHP that the time zone would be set? And is there a place in WAMP that set the time zone, or is done in php alone?

Thanks for any assistance. Troy

  • Are you sure that the difference is based on the timezone and not on a missing NTP sync? Is it bigger than some minutes? – Nico Haase Mar 29 '18 at 14:27
  • 1
    Yes, the difference in approx 5 hours, which indicated to me it is a timezone problem. – Troy Marker Mar 29 '18 at 14:31
  • Okay, good to know. Then you should decide whether you want to store the data in GMT and do the conversion in your application or not. After all, how **exactly** do you insert data? – Nico Haase Mar 29 '18 at 14:34
  • Is use the CURRENT_TIME function in the sql I send to the server from my app. – Troy Marker Mar 29 '18 at 14:37
  • I'm not sure of what you mean with `localhost` (it's just a standard alias for "current machine") but two programs that run on the same machine will use the same clock and two programs that run on different machines will not. Whatever you're trying to do you need to ensure you're only using a single clock for calculations. – Álvaro González Mar 29 '18 at 14:45
  • I am working on the app on my home pc so they that same clock is being used. – Troy Marker Mar 29 '18 at 15:13

2 Answers2

1

Well, I found a solution. I used the date and time functions of PHP to adjust the time to a correct value before sending it to the database. Not the solution I was hoping for, but it will get the job done, and I can move on with the project. Thanks to those who helped.

  • 1
    Just want to post an update. I found the solution to the problem. I forgot to mention that I was using CakePHP to develop my application. I did some research and found that CakePHP set the default time zone in its configuration. Go figure! – Troy Marker Mar 30 '18 at 10:59
0

By using CURRENT_TIME, the time settings of your SQL server are used, and not the settings you wrote into php.ini. By setting the timezone as described at How do I set the time zone of MySQL?, you might achieve the proper values.

Keep in mind that these should be kept in sync: doing some calculations in PHP and some in your database increases the risk of miscalculations. If you use PHP's date (or resembling functions) everywhere or MySQL's time functions, this is more safe

Nico Haase
  • 11,420
  • 35
  • 43
  • 69
  • I set the timezone in MySQL as suggested, I am still getting the -5 difference in the times. I am using phpMyAdmin to view the database, could this have some thing to do with the problem? Also, would using php date function be better? – Troy Marker Mar 29 '18 at 15:05
  • How did you set it? And can you inspect the database using another tool (to rule out that there is a problem with phpMyAdmin)? – Nico Haase Mar 29 '18 at 15:18
  • 1
    In the punch in routine, i changed everything over to use the PHP date function. It still saved it with the 5 hour difference. I am currently going through my configuration on my webserver, database and php to see if I can find any other place that could cause the problem. – Troy Marker Mar 29 '18 at 17:31