4

I live in Denmark - but am setting up a page for a friend in USA (Washington State). The page is hosted at Surftown, in Denmark.

I know there is a 9 hour difference, so I set:

date_default_timezone_set('America/Los_Angeles');

But there is something I obviously don't quite understand about time zones / date() and strtotime() because:

Via text input I am trying to save a specific date and time to the database.

Lets say that $_POST[date] input is: '01/29/2015' and $_POST[time] input is: '02:00 PM'.

I then create a stamp using:

strtotime($_POST[date].' '.$_POST[time]);

But when I try to output this, I get the correct date - but 9 hours is added to time? Why is this?

I guess I could just remove the time zone setting for this specific task - but I'd like to understand why. I am setting the time zone because I also need to save some timestamps based on the actual time of the user (in Washington state - not Denmark).

Can you help?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 3
    I can see all sorts of trouble here once Daylight Saving rears its ugly head. Set a consistent internal time for the server and any database - UTC is recommended - and convert to local time on output. Today you have Washington State and Denmark. Tomorrow you might have to add London or Washington DC or, heaven forbid, somewhere in Indiana. Adding additional timezones will be much easier this way. –  Jan 29 '15 at 23:39
  • Thanks. That makes sense. Except... Im not sure I know exactly what you mean by "convert to local time on output". 1) How do I know what the local timezone is? and 2) Would I then use date_default_timezone_set() when outputting? – John Kjøller Jan 30 '15 at 00:37
  • Please clarify how you're "trying to output this" exactly, and what timestamp you get from your `strtotime` exactly. – deceze Jan 30 '15 at 01:22
  • @JohnKjøller The most reliable way to identify the timezone is to ask the user. Alternatively, you could emit UTC and have Javascript convert to local time based on the client's machine settings, or you could use GeoIP on the server to locate the client and set the display timezone from that. Don't mess with `date_default_timezone_set()`. Use it to set UTC at the top of your script and forget about it. Use the `DateTime` class for all your date/time operations. It includes methods to change the timezone for a `DateTime` instance. –  Jan 30 '15 at 02:00
  • @HoboSapiens Thanks again. So lets say I ask the user. How do you suggest I set the time zone - if not using date_default_timezone_set() ? – John Kjøller Feb 01 '15 at 18:56
  • @deceze WHen I "try to output this" - I use date(). Like this: date('m/d/Y H:ia', $myTimestamp); $myTimestamp is coming from strtotime(). Like this: strtotime($_POST['date'].$_POST['time']); The date and time inputs are formatted: mm/dd/YYYY and tt:mm AA – John Kjøller Feb 01 '15 at 19:09
  • @JohnKjøller If you work with `DateTime` objects you can use the [DateTime::setTimezone()](http://php.net/manual/en/datetime.settimezone.php) to set the timezone for an object and [DateTime::format()](http://php.net/manual/en/datetime.format.php) to format the result for display. –  Feb 01 '15 at 20:09
  • 1
    @HoboSapiens Got it! Thank you so much for taking the time to help me out. Im not sure I know exactly the right way to do this: Are you going to write an answer that I can accept, maybe? – John Kjøller Feb 01 '15 at 23:48
  • what server are you using and what do you get when you look at the time. If you're on a linux machine, issue> date command to see what you get... – unixmiah Feb 03 '15 at 01:22
  • Thanks all. As mentioned: @HoboSapiens asnswer helped me out. I am not sure how things work here at Stackoverflow - and I dont want to disrespect you guys... should I do anything to give credit to hobosapiens? – John Kjøller Feb 08 '15 at 23:46

1 Answers1

0

strtotime assumes you are passing a UTC date so it converts it to the server timezone, what you can do is the following:

$date = new DateTime($_POST[date].' '.$_POST[time], new DateTimeZone("UTC"));

echo $date->getTimestamp();

VictorCL
  • 49
  • 1
  • 4