0

I think I'm going nuts as this has to be simple.

The time on my computer is currently: 14:41 When I run any of the code below, it returns: 13:41.

$date = new DateTime();
$date->format('Y-m-d H:i:s') //13:41
date('Y-m-d H:i:s') //13:41

date_default_timezone_get() returns UTC.

Now I understand why it's an hour behind (because we are in daylight saving time) BUT isn't there a function that takes all this into account and returns the actual time?

p.s I am expecting to have missed something obvious here.

Edit: date('Y-m-d H:i:s -- I -- e'); returns 2013-09-13 14:15:47 -- 0 -- UTC. Shouldn't the I param be return 1?

webnoob
  • 15,747
  • 13
  • 83
  • 165
  • Check this : [How to check if a time offset is in daylight savings time?](http://stackoverflow.com/questions/11181667/how-to-check-if-a-time-offset-is-in-daylight-savings-time) or [PHP daylight saving time detection](http://stackoverflow.com/questions/6155224/php-daylight-saving-time-detection). There is also [PHP: date_offset_get](http://php.net/manual/en/function.date-offset-get.php) – Fabien TheSolution Sep 13 '13 at 13:46
  • 1
    If you change your timezone in php.ini that should do the trick. http://stackoverflow.com/questions/2953748/system-time-is-different-than-apache-timestamp – Lumberjack Sep 13 '13 at 13:50
  • @Lumberjack - Trying that now, thanks. – webnoob Sep 13 '13 at 13:52
  • Changed the option, restarted apache (using Xammp) and it's still one hour off. Is there something in Apache that needs changing as well? Note: changed to `Europe/London` from `Europe/Berlin` – webnoob Sep 13 '13 at 13:53
  • Also, isn't `UTC` meant to avoid all these issues? – webnoob Sep 13 '13 at 13:54
  • Are you running this script on your machine or a different server? – Pitchinnate Sep 13 '13 at 14:07
  • Both. I have it running on localhost and an external server which both return the same results (although I have only changed the ini file on my localhost). The issues comes about because I am passing the time down to a web service call that returns data based on when it was last accessed. The time calculated by the web service is correct so the time diff is always 1 hour off. – webnoob Sep 13 '13 at 14:09
  • This may not be your problem, but your host may not have the time correctly configured in their end. I sometimes have to ask my host to correct the time on their server instances. – MrSynAckSter Sep 13 '13 at 14:19
  • It's running on localhost so I `assume` my host time is my computer time (unless Xammp is doing something with it). – webnoob Sep 13 '13 at 14:20
  • have you tried setting your default time zone in php? `date_default_timezone_set('America/New_York');` – Pitchinnate Sep 13 '13 at 14:27
  • Praise the lord it worked! How can this work but the setting in the ini file not?! Either way, can you post this as an answer please :) – webnoob Sep 13 '13 at 14:29
  • 1
    It's possible that your PHP install isn't using the php.ini that you think it is. Run phpinfo() to find out. – Alex Howansky Sep 13 '13 at 14:30

1 Answers1

1

Don't rely on php.ini settings. If you want control over the timezone, control the timezone:

<?php

$date = new DateTime();
$date->setTimezone(new DateTimeZone('Europe/London'));
$date->format('Y-m-d H:i:s e')

Replace "Europe/London" with whatever your definition of "actual time" is.

As an aside: if date_default_timezone_get() returns UTC, then the output of $date->format() is the same on every machine with a correct clock. That's the point of UTC.

Peter
  • 29,454
  • 5
  • 48
  • 60