sanchitkhanna26
sanchitkhanna26

Reputation: 2243

date() function outputting wrong result - PHP

Here's what i did -:

  1. Generated the UNIX_TIMESTAMP result in mysql. It came out 1360756718 seconds.
  2. Since I am in GMT +5.30, got the number of seconds in 5 hrs and 30 mins. Result was 19800 seconds
  3. Ran this function -> date('j M Y H:i:s',(1360756718+19800)).

But unfortunately, the answer was 1 hour ahead of original time. And I mean exactly 1 hour. Result was 13 Feb 2013 18:28:38 which should have been 13 Feb 2013 17:28:38.

Where am I worng?

Upvotes: 0

Views: 150

Answers (6)

Klaus F.
Klaus F.

Reputation: 145

the other answers say you should check and correct your timezone settings. I would do that because it's more correct (regarding to daylight saving time and so on).

If you want to have the easy way try gmdate function:

string gmdate ( string $format [, int $timestamp = time() ] )

This one will always calculate in GMT - you don't have to check the server settings. (However it will ignore Daylight Saving Time too.)

Upvotes: 0

Vince V.
Vince V.

Reputation: 3143

You could try adding:

ini_set('date.timezone', 'My/Timezone');

Since php 5.1 it's also possible to do:

date_default_timezone_set ( string $timezone_identifier )

http://www.php.net/manual/en/function.date-default-timezone-set.php

To set the time to your own timezone. The timezones that are available for php are here:

http://www.php.net/manual/en/timezones.php

Upvotes: 0

Mayukh Roy
Mayukh Roy

Reputation: 1815

Please check your current timezone first. Here you can check all supported timezones as well

Upvotes: 1

Peon
Peon

Reputation: 8030

echo date( 'd.m.Y H:i:s', '1360756718' ); // 13.02.2013 13:58:38
echo date( 'd.m.Y H:i:s',  ( '1360756718'+19800 ) ); // 13.02.2013 19:28:38

For me this came out as planned. Are you sure that you started with the right time? As some already mentioned, the problem most likely lies in your timezone or daylight savings time.

Upvotes: 1

TheBrain
TheBrain

Reputation: 5608

Well, you are in +5:30 but with daylight savings time, we still are 1 hour back. so you now are only +4:30

Upvotes: 3

Husman
Husman

Reputation: 6909

I think your timezone is set incorrectly. You need to dig around your Apache/PHP/Mysql settings to find out what it is set to.

Upvotes: 1

Related Questions