I have a datetime object received from server in the following format:
{"date":"2021-05-11 13:02:01.273000","timezone_type":3,"timezone":"UTC"}
I'm having trouble to get the correct date corresponding to the user browser how can I parse this date into a js date object with the right time zone calculations. I used to take only the date disregarding the time zone and it turned out to be wrong to the user
Try using toLocaleString method and specify the timeZone:
x = JSON.parse(`{"date":"2021-05-11 13:02:01.273000","timezone_type":3,"timezone":"UTC"}`);
new Date(x.date).toLocaleString("en-US", {timeZone: x.timezone})
Related
Anyone can explain to me in which format are these DateTime strings from OpenWeatherMap API and how to work with them in order to:
Just see the time (Greenwich +1)
sun rise="2019-01-28T06:31:03" set="2019-01-28T16:14:39"
View the Date & Time (Greenwich +1)
lastupdate value="2019-01-28T11:20:00"
Those dates are in ISO 8601
The examples you actually have do NOT contain any TimeZone information so they could be any timezone..
If you work with datetime offsets (that include time zone information) you would typically see something like this
2008-09-15T15:53:00+05:00
The reasone it is formatted in ISO 8601 is so that the date time picker (that is the actual format you use for the value) on your HTML page knows how to interpret the time into your local (machine clock configured time)
Typically if using UTC you will have a date time that looks like
2008-09-15T15:53:00Z
Which is Zulu Time
And as i mentioned already if it does not have any of those it is assumed local time at server that generated it.
Thanks, I was able to convert them by using concatenated date and strtotime functions.
date('H.i',strtotime($sunset_time));
The local time on the server is Greenwich, whereas I would need to add +1 for my time zone (Europe/Rome).
In the end I was able to add +1 hour like this:
date('H.i', strtotime($sunset_time) + 60*60);
Is this a correct way?
I am facing a problem with unix timestamps, php and mysql and would be great if somebody could explain to me where I am going wrong or if I am not then why I am getting the figures that I am getting.
When I use jquery datepicker to pass the date in year-month-date format to php the hour and minutes have been set by default of 23:00:00 in the timestamp even though I am not passing this infromation in the request. So my question is where is this phantom 23:00:00 appearing from?
Workflow:
Using datepicker: datepicker -> php -> mysql = TIMESTAMP which has time set at 23:00:00.
Without using datepicker: php->mysql = TIMESTAMP with the correct hour and minutes.
Thanks for reading.
EDIT: PHP code as requested:
PHP code:
$setdatealpha = $_POST['datepickeralpha'];
$setdatealpha = strtotime($setdatealpha);
// With this, I am inserting into MySQL like so:
$sql = "INSERT INTO TABLE (DATE_FIELD) VALUES (?)";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s',$setdatealpha);
$stmt->execute();
Now when I read the entered information back and convert it to date time format via date('Y-m-d',timestamp), the date entry correct and the time entry has the 23:00:00 value.
This does not occur if I do a standard converstion via strtotime (date);
Based off of the information currently available, I would suggest that you make sure each timestamp is in UTC. I always run into timezone issues.
For PHP, like: $current_timestamp = strtotime($date." UTC");
For jQuery datepicker I found this stackoverflow thread: How to obtain utc time in jQuery datetimepicker
Most likely, time zone.
First of all, let's clarify the context. strtotime() produces a Unix timestamp, which you apparently feed DATE_FIELD with. If that works, it means that the column is an INTEGER. In the case, you're doing something afterwards to display the date and you haven't shared that part—also, MySQL is innocent here because it doesn't even know what DATE_FIELD is meant to be date.
While strtotime() can be fed with a raw date, it needs to generate time as well. It can't do it unless it knows the time zone. Additionally, when you have an integer variable with a Unix timestamp and you want to display it as proper date you also need to know the time zone.
In both cases, if you don't provide it PHP will use a default value:
var_dump(date_default_timezone_get());
So you'll possibly want to set a known one with e.g. date_default_timezone_set(). However, your users may have a different time zone than you so yours would be meaningless to them. Since you prompt the user for a raw date (without time) it's possible that time is actually not relevant to the question. In such case, you may want to:
Make DATE_FIELD of DATE type.
Avoid strtotime() and similar stuff. You may want to use checkdate() instead.
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
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();
i need help with multi timezone website in codeigniter.
config.php:
$config['time_reference'] = 'gmt';
date_default_timezone_set('UTC');
now when user want to see event(website is for events) it shows dates to his timezone with gmt_to_local() function, this works fine..
Now problem is when user want to add new event, he picks date and time, now it gets converted to timestamp:
strtotime($event_date);
And problem is that he picks date and time for event on his timezone, now i need to convert it to gmt, and save to db.
Example, if user is in UP2(+2:00) and he set 12-25-2012 22:30:00 it will be converted to timestamp and saved in db, but it is incorrect, it should subtract 2:00 and save that timestamp to db as timestamp of 12-25-2012 20:30:00 (this is converted to gmt)
Hope that you will understand.
Any solution for this?
Thanks.
In your event form, you should send to the server a hidden input witch will contain client's timezone offset. You can get it via Javascript's Date.getTimezoneOffset() method (it gives you offset in minutes between UTC and users's local time).
If you can't do that, you can save user's local timezone into db or ask it in your event form.
Then, in your INSERT or UPDATE query, you can use DATE_ADD('2012-12-13 19:41:00', [user offset] MINUTE) to convert timestamp. You can also do it in php, or client-side when submitting form...
For example in PHP :
strtotime($event_date);
gives you the date's Unix timestamp (so, in seconds). To convert it into another timezone, according you get the offset in minutes with javascript method, you can do :
$dbTimestamp = strtotime($event_date) + $_POST['offsetInMinutes']/60;
If you get the offset in this form : '+2:00' or '-6:00', you can get offset in hours using a regular expression :
preg_match('/(.*)\:00/', '+2:00', $match)
will set '+2' into $match[1]