Arnold Porche Villaluz
Arnold Porche Villaluz

Reputation: 213

PHP Date not accurate

I have this code:

$date = date("m/d/Y H:i:s");

But it displays 1 day advance for date and 5 hours late for time from my computer time.

For ex: My date/time is 07/25/2011 10:12PM

this code displays: 07/26/2011 05:12:43

What could be the problem?

Upvotes: 1

Views: 7671

Answers (6)

Ray Toal
Ray Toal

Reputation: 88378

As you commented that you were in the Pacific time zone, and your call to get the time returned something 7 hours ahead, and it is July, you got the time in GMT.

While you can and probably should use date_default_timezone_set as suggested by PaulPRO, do try to consciously think of timezones. Always.

In applications involving dates and times, it is important to be aware of two concepts:

  • instants
  • local dates, local datetime, local time, ...

An instant is a point in time, and is the same instant for everyone in the world. It is usually represented as a number of milliseconds since the epoch, but can also be represented as a date with a time zone. A local {date, time, datetime} is something without a timezone. Something like "July 26" or "5:15 pm" or "July 26 at 10:29 p.m." Okay but what timezone?

When you deal exclusively with local dates (or local times or local datetimes) you run into these kinds of problems. As much as possible try to work with real instants.

End of rant. :-)

Upvotes: 0

Garrett Smallwood
Garrett Smallwood

Reputation: 389

Instead of using this function to set the default timezone in your script, you can also use the INI setting date.timezone to set the default timezone.

If your PHP scripts do not show the correct time, the reason is that most probably your hosting server is in a different timezone. This can be easily resolved by changing a setting in PHP called date.timezone.

Depending on your location you can set a specific date.timezone value in PHP using the following option which should be added to your local php.ini file:

date.timezone = "US/Central"

The above example assumes you'd like to set the timezone for your PHP scripts to US/Central. The full list of supported time zones is available here and you should simply replace "US/Central" with the desired timezone.

Upvotes: 0

Jason Kaczmarsky
Jason Kaczmarsky

Reputation: 1686

Is the server local? If it is you have to change PHP's timezone setting (in php.ini) to match your own which will change it across the entire site. Or you can change is on a single page with date_default_timezone_set

If the server isn't local, then the timezone is based on wherever it is located which you can change if you have access (You won't if you bought it from a provider and it is shared).

Upvotes: 0

Frank Harper
Frank Harper

Reputation: 3176

It sounds like the timezone on the computer which you are running PHP is set incorrectly. Correcting the problem depends on the OS you are using.

Upvotes: 0

cwallenpoole
cwallenpoole

Reputation: 82028

I'll wager that this is a timezone setting issue. What happens when you echo date_default_timezone_get();. I'll guess that it is something which is 7 hours off from your current timezone's time.

Upvotes: 0

Paul
Paul

Reputation: 141829

It is probably running on a server in a different timezone than your computer. You can use date_default_timezone_set to set it to the correct timezone before calling date(); Find the correct timezone identifier for your timezone from this list: http://www.php.net/manual/en/timezones.php

Upvotes: 3

Related Questions