I have a jQuery datepicker for a user to select a dueBy date (mm/dd/yyyy) that gets stored in a MySQL database. The user can successfully select a date in the datepicker and submit/save the record. I use the following method to convert the m/d/Y to a UTC format:
$date = DateTime::createFromFormat("m/d/Y", $_POST['dueBy']);
$values['dueBy'] = $date->format('U');
Then when the user views the record again, I convert the UTC time back to an easily readable format:
$dueBy = date('m/d/Y', $aQ->dueBy);
I fill the datepicker with the formatted dueBy date where the user can either leave it as is, or can select a new date.
The issue is this: upon updating the record, the date shifts back one (1) day each save. E.g. if it's originally 03/03/2015, it will save to 03/02/2015. If I open and save the record again, it saves as 03/01/2015.
This is the code using to save (exactly the same as when we create the record):
$date = DateTime::createFromFormat("m/d/Y", $_POST['dueBy']);
$values['dueBy'] = $date->format('U');
I've viewed the record each time to view the values, and they aren't decreasing consistently (e.g. one value has reduced by 86390, and then a few minutes later it had reduced by 86368).
Can anyone explain why this is happening, and how to avoid it?
Related
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 have not got any code built yet as I need to ask this question before I can start making it.
first what I am doing:
I am going to be making a tournament system on my website and I would like the tournament creators to choose a date and time that tournament will be active for signups and closed for signups.
I believe I will be using www.jongsma.org datepicker as I think it is very nice looking and easy to use for the end user. Link:here
The Question:
After sanitizing the input from the forms date/time do I need to specify for it to be converted from the users (Person inserting the time) local time to UTC before I store the data on the database or does it automatically convert the input from there local time to UTC when the data is being written to the database?
I am using MySQLite
MySQL accepts datetime in this format "Y-m-d H:i:s".
You can always convert different formatted dates into unix_timestamp with strtotime and turn it into mysql date format with:
$unix_time = strtotime($differentFormattedDate);
date("Y-m-d H:i:s",$unix_time);
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]
I had used javascript calender in my form. User have to input a date using this calender. And date is stored in the database but what I need is that only day of a date must be saved.
How can I do that as I cannot make changes in javascript code as i m not good at it.
$date_customer=date("d",strtotime($_POST['datum1']));
I had also tried it by changing the column name to "tinyint" but didn't work :( .... it only stores 127 and shows 1 when record is viewed from database.
Instead of sending date to server you could send the day by using
.getDay() method of javascript Date object.
I dont know the format of your date you get in your text input (when you click on one of the days in your calendar) but i'd suspect it to be dd/mm/yyyy or mm/dd/yyyy
So your php will need to be the following to only get the day
$date = explode("/",$_POST['datum1']);
// if format is dd/mm/yyyy then use the below
$date_customer = $date[0];
// otherwise if format is mm/dd/yyyy then use the below
$date_customer = $date[1];
Check out the explode function
i would save the date in MYSQL as an INT by using this function (save it as a unix timestamp) which would be helpful in comparing dates later on (up to the second) or add/remove days/years/months .
the idea would be send the whole date string generated by javascript to the PHP script "dd/mm/yyyy" ,
then in php using the explode function and create the unix timestamp using the mktime function
then save it to the database as an int ,
then when you want to read it , use the php date function to know the day/month/year/hour/second/minute , you could then also add hours (+3600) or days (+3600*days) etc... , or even get range of dates and many other functionalities you may use later ...
cheers
I suppose that you use mysql (TINYINT are mysql specific).
TINYINT are integer and in php integer are usually not prefixed by zeros
Use a DATE field and format it when you report it.
You can use mysql date_format(date,"%d") function in your query.
Or the php date function.
select date_format(date_field,"%d") from some_table;
You can use a VARCHAR(2) to store the date (ugly).
If you stick to store only the DAY in an int or tynint.
use sprintf("%02d",$day) to format it correctly in php.
is there any easy to validate a time field entry against the current date and time?i get date and time entry from an user in two separate fields. I used jquery date picker and time picker for both. with the date I had the option to show current date and future dates and not the past dates so it's good. with the time field i have to show all the time but want to somehow validate to see if the time is already gone for that date.I can o away from jquery and just use php if possible. any ideas?g
You can do
$ispast = strotime($field_value) < time();
This will tell if the given time is in the past. The only requirement is that $field_value is in a format that strtotime recognizes. This will interpret the date in the default timezone you have set (see date_default_timezone_set).