aradhya
aradhya

Reputation: 355

Mysql Current Timestamp and time() showing different values

OK. So I have been trying to implement a timer. Now a very weird thing is happening and I can't understand why ?.

Basically I am trying to find the difference between the last access and the current time. I am storing the time of last access in the database. This value is according to the server time. But when I try the time() function of php it shows me values which are 5-6 hours behind the time that I have in the database.

For example: here is my code :

    $t1= strtotime($played_row->timer); // Time from the database with CURRENT_TIMESTAMP

    $t2= strtotime("now"); // Get the current time

It shows Year: 2012 Month: 01 Day: 21 - 05:28 pm for t2

and Year: 2012 Month: 01 Day: 21 - 10:28 pm for my timestamp values.

Can anyone tell my why is that ?

P.S: I am running the code on my computer itself.

Upvotes: 0

Views: 1237

Answers (3)

Fabian
Fabian

Reputation: 3495

Try using date_default_timezone_set() to set the timezone in PHP that is used in your database.

date_default_timezone_set — Sets the default timezone used by all date/time functions in a script

Alse see date_default_timezone_get() how to get ini-set timezone.

Upvotes: 1

nicolaskruchten
nicolaskruchten

Reputation: 27370

Most likely this is a timezone issue: if you are in the Eastern timezone, you are 5 hours away from UTC right now. If one sytem is returning local time and another is returning UTC this is what you will see.

Upvotes: 2

Treffynnon
Treffynnon

Reputation: 21553

At a guess I would say that your database and PHP are using two different timezone offsets.

Upvotes: 4

Related Questions