3

I have some PHP code that inserts a date into a table:

INSERT INTO tblEventLog VALUES  ...  date("Y-m-d H:i:s",time())  ...

The results of this are usually correct, but the occasional date is an hour behind:

315070  05-Sep-10 18:08
315069  05-Sep-10 18:07
315068  05-Sep-10 18:07
315067  05-Sep-10 18:06
315066  05-Sep-10 18:06
315065  05-Sep-10 17:04
315064  05-Sep-10 18:01

What could be causing this? There is only a single server.

EDIT:
Using NOW() Worked!
The problem was that one of the PHP pages was changing the timezone (when creating a RSS feed) and the PHP time() function was picking that up. Using the database to set the time fixed things.

soupagain
  • 1,123
  • 5
  • 16
  • 32
  • 1
    What if you leave out `time()` and just say `date("Y-m-d H:i:s")`? – BoltClock Sep 05 '10 at 18:03
  • 1
    Are all of your requests coming from within the same time-zone? – fredley Sep 05 '10 at 18:03
  • 1
    @fredley: the timezone is determined by the server on which PHP is running, not by the requests. In this case there's only one server, so my guess is only one timezone is involved. – BoltClock Sep 05 '10 at 18:06

1 Answers1

2

I'm not sure what could cause this. I'd recommend switching the column data type to DATETIME, though, and if you're just inserting the current time then use MySQL's NOW() function rather than grabbing the time in PHP. The more you can do directly in the database the better. Doing this might obviate this bug entirely.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578