Advice for php time zone settings - php

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?

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();

Related

how to set local time in project irrespective of server location

I have my project based in India and my hosting server located somewhere that is at a difference of -5:30 HRs from Indian time.
So my time being stored in database is coming as based on there timings. How can I rectify this in PHP so that the time actually gets stored on India.
I tried using date_default_timezone_set('Asia/Kolkata');
but still the time gets stored with a difference of -30mins to -45mins. What could be possible code fort his so that the time sets exactly according to Indian timings. Any advice will be very helpful.
Step-1: Set timezone as asia/kolkata
Step-2: Now, first store the
date in local variable, example: $mydate = date('Y-m-d H:i:s'); and
just print the variable value to see, if it is the time as you want?
Step-3: If yes then store $mydate variable value in database.
You can set the timezone in the database.
Few more threads of the same topic are here:
MySQL Time Zones
Set MySQL database timezone to GMT
MySQL timezone change?
If you want to store the current date/time on your database and use the timezone you have. Try to use now() function.
Reference Link: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_now
As you said your server is running at -5.30 hours from India, get the dates like this:
date('d-m-Y H:i:s',strtotime('+330 minutes')); // this adds 5.30 hours to the current time.

Render date from date field in correct timezone?

Im have a field with date time type. I see in database is always saved other timezone than my default.
When node content is rendered, date looks fine, but when i trying to get node from code i cant render date in correct timezone.
$node->field_customtime->getValue()
I have array of values with standard timezone, when i dump value, the time is wrong.
So i was trying to do it like that:
$value = $node->field_customtime->getValue();
$value = $value[0]['value'];
$date = new \DateTime($value);
$date = $date->getTimestamp();
echo \Drupal::service('date.formatter')->format($date);
And there is still raw date from database (wrong).
I dont know how to correct display date from custom date field. I see in the form correct date (i.e. 11:00 european time) but in database is 10:00. On node page is correct 11:00 so drupal convert it somehow, but how??
I will assume you are using MySQL?
If so then unless you are saving a timestamp the timezone does not matter. It should be saved exactly how you sent it.
If it is a timestamp then the date is automatically converted into UTC for storage and then converted back into whatever timezone you have set in the mysql configuration.
MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)
What you need to check first is how you are saving the date. Are you storing a timestamp?
If not you need to check how the data is saved and see if anything is happening to the date before inserted into the database.
Unfortunately I do not know much about Drupal but after a quick google I have found out that Drupal 7 happens to have a few ways of handling the datetime. It is quite possible that this may apply for Drupal 8 and you simply need to adjust a configuration.
https://drupal.stackexchange.com/questions/3613/the-differences-between-date-time-zone-handling

Retrieved date, time off by 7 hours?

I am having trouble displaying the correct date, well actually its only the time.
In the db the date is stored in this form: '2012-09-28 23:30:00' (off by 7 hours of actual date time)
That is the value for a date of an event.
When I retrieve it using this script(for drupal):
$ttdate = $obj->field_date_test_value;
$nttdate = strtotime($ttdate);
$okdate = format_date($nttdate, $type = 'medium');
print $okdate;
...it works fine but the time is off by 7 hours. So instead of showing 'Fri, 9/28/2012 - 4:30pm' it shows 'Fri, 9/28/2012 - 11:30pm'.
Note that on the event's page, drupal retrieves the correct time...
I did some research and I figured it has to do with the timezone? But I have the timezone set to Los Angeles so I am not quite sure what is going on. Maybe its my script?
I assume that you use the Date module.
This is probably because the Date module saves the date in UTC and alters the output on rendering (using format_date(), I think). Since you do not format the date in your own code, you see the raw UTC time (which is PDT + 7 hours) even though the correct date is displayed by the event page.
The bottom of this page explains how timezones are handled in the Date module. My guess is that either the first or third scenario is relevant in your case.
Try the following in mysql:
set time_zone = '-07:00';

Does JavaScript Date object recognises the input datestring as UTC time or local time?

There are already quite a number of questions regarding time, but I still couldn't find one that can answer my question. So, I have to apologise if this is a repeat. My question is this:
I have a MySQL table column named postDate in datetime format. My server is on system time (UTC). I have the following php script:
<script type='text/javascript'>
var expiry = new Date('$postDate');
alert(expiry);
</script>
$postDate = date("r", strtotime($postDate)); // sorry forgot to add this line
I just want to know, assuming $postDate is 2012-03-30 01:00:00, and the client is on a +8hr time zone, what will he see on the alert? I.e. does JavaScript Date object recognises the datestring as UTC time or local time?
Depending on the user's locale, that will fail with the result "Invalid Date".
Instead, you should pass a timestamp. Timestamps are ALWAYS in UTC. To get a timestamp from your datetime format, run it through strtotime(). But bear in mind that JS does timestamps in milliseconds, not seconds, so you have to add an extra 000 to the end.
So basically:
$postDate = strtotime($postDate)."000";
The result of the alert will still depend on the user's timezone, but it will be correctly offset from the UTC timestamp you gave. For example, if the timestamp is 1AM UTC, but the user is in UTC+8, they will see 9AM.
and yes, it does work, amazing. just 2 lines of code in a fiddle.

How do I correctly display time in Drupal using the Date module?

When creating a new Event on my Drupal site I set the date/time of the event to what I assume is local time. I expected the date/time in the system to be set to the value I saw on the add/edit form, but its not; it's set to UTC.
Event Start (add/edit form): 2012-12-20 1:00pm EST
Event Start (database): 2012-12-20 6:00pm UTC
When I go to display this date/time using date() even after setting the default timezone to EST ("America/Toronto") the date/time is still showing 2012-12-20 6:00pm.
The problem doesn't necessarily seem to be drupal, the date/time is being displayed in the way it's being stored, but I'm thinking the problem may be the date/time entry control on the add/edit form...possible javascript time related.
The values are being posted as they're displayed: 5:00pm goes to the server as 3 distinct values: 5, 00, PM. They're being stored in the database as UTC, but not matter what I try they're always displayed as UTC. I've tried format_date, date_default_timezone_set, setting the PHP timezone in the .htaccess file...no luck :s
Hate having to do it manually, but this works:
$tz = new DateTimeZone('America/Toronto');
$offset = $tz->getOffset(new DateTime($start_date));
$start_stamp = strtotime($start_date) + $offset;
If you have a date formatter (config page at /admin/config/regional/date-time), you can use the machine name to reuse it:
$date = new DateObject($timestamp);
$value = date_format_date($date, 'my_date_string_format');

Categories