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);
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 want to store the date and time that a user performs an action on my website into a MySQL database. I'd like to be able to do the following with ease:
Store the date and time as one field in the database
Use a built in PHP or MySQL function to generate the date-time of the action
Store the date-time based on my server's time, and not worry about user timezones.
Order By the date-time field when I query MySQL
Later, display the date-time in many different formats using built in PHP methods
Here are my questions:
What data type should I use in MySQL ( eg. timestamp, datetime ... )?
What method should I use to generate the date-time ( eg. MySQL's now(), PHP's date() ... )?
What PHP method should I later use to format the date-time in various pretty ways ( eg. 23/4/2012, 5pm on Monday, July 2012 ... )?
I would store it as a datetime, not a timestamp.
I normally use the PHP date function and that way if you ever want to store the time relative to the user's timezone you can simply change the timezone based off the user's settings.
When you pull it out of the database, use strtotime() to convert it, then you can use all the date() features to display it however you want. Example:
echo date('F j, Y',strtotime($db_datetime)); //Displays as 'March 5, 2012'
I've struggled with this question for years, and I'm beginning to think that the best way might be to store the time as an integer that represents Unix time (number of seconds from Jan 1, 1970). I've done this and it works fine.
Personally I've never used datetime, and I can't think of a situation when I ever would use this. It just carries too many problems with it.
Timestamp is a lot better, but in MySQL it can't store a date later than 2032.
I would love to hear some serious discussion on this topic, but Stack Overflow might not be the best place for this.
If you set the mysql data type to a non-nullable timestamp, then save rows with a null value for that column, mysql will automatically update the timestamp for you.
As for reading it back out again, you can just use php's strtotime and the date object to get it into the format you need.
You should use the datetime datatype for your requirement.
It will store both the date and time from your input field based on your query.
For retrieving the datetime you can use the mysql's date_format() function or PHP's date() function.
The datetime will always be stored according to the server's time and not on the clients time.
I am creating this post, to get confirmation that i am understanding the process correctly, and also to get feedback on a few minor details on solving the issue of post times being displayed and stored correctly for users in various time-zones.
Storing the time
First step is getting the users timezone as a GMT offset format using a javascript function, and storing it in a session variable. For example, the $timezone variable will store GMT -4
$timezone=$_SESSION['time'];
Next, Once i have my users timezone. When the user makes a post, I store the post time in mysql database, I will need to store it as a UTC format. How do i do this? Currently when I store the time data. it is in the following way. My stamp field is datetime format, and i would like to leave it as such if possible.
INSERT INTO posts (stamp) VALUES (now())
What function do i use instead of now() to get the UTC format which will then be inserted into my database? I assume that I need to use a php function which will use the $timezone to produce the UTC date.
Displaying the time
Next, Once the UTC date is stored in the db. I need to display the data to the user based on the $timezone variable we set.
So when I display the time, I currently do
echo date('F d', strtotime($list1['stamp']));
Once i have stored the data as UTC time, this will display the UTC time, but i need to show the user the UTC offset for their timezone, so I will need to convert $list['stamp'] in UTC time to a the users timezone using $timezone. What function do i user for this?
tl:dr
This should be all I need to make this work. let me know if you see any suggestions, or items that i have not accounted for, and if you know what functions I need to use to convert the time to UTC to store in the database, and what function to use to convert the UTC time to display user, using the $timezone variable.
For inserting UTC time into the database:
INSERT INTO posts (stamp) VALUES (UTC_DATE())
UPDATE: This will only insert the YYYY-MM-DD into your database. If you need time as well, then use:
INSERT INTO posts (stamp) VALUES (UTC_TIMESTAMP())
Then for printing the date according to timezone:
$date = date_create($list1['stamp'], timezone_open($timezone)); //$timezone='Pacific/Nauru';
echo date_format($date, 'F d');
That should help get you started. Learn more about PHP's DateTime Class for Object Oriented Methods, and cleaner programming in general.
i would like to store the date and time in the following format in the database
a) DD-MM-YYYY or i could add up HH-MM
b) as my servers are located in US i would like to fetch the time as per IST. now that will be GMT : +5:30
earlier i used to store the date in mysql in this format timestamp(int(11)):1291746600. i used to convert the date with strtotime();
currently my intention of storing the date is just to store and display. in the future i would like to calculate the no. of days, months etc.
which would be the best possible solution for this?
P:S : i would appreciate if someone could explain me which datatype to use and how to use it with PHP.
Use DATETIME fields! They are the best format to store dates in mySQL. They offer proper indexing and optimization, and you can use the full range of mySQL's date functions.
Any specific format you need to output the fields in, you can create from a DATETIME field using DATE_FORMAT().
MySQL doesn't support time zones in DATETIME fields - you will usually set a global time zone on the server and use that.
There's good related reading on Timezones in these questions:
MySQL: keep server timezone or user timezone?
Lost in dates and timezones
Use a datetime field.
http://dev.mysql.com/doc/refman/5.1/en/datetime.html
You will have access to a lot of function for date manipulation.
If you want still to use a varchar use the ISO-TIME format (YYYY-MM-DD) not the us.
I think you should use DATETIME data type.
For more operations on date and time have a look at functions listed here
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
I have found a proper solution to my "problem" but even after reading mysql pages, I don't understand the logic behind it.
I currently store registration information in my system in a "datetime" formatted field in one of my tables (YYYY-MM-DD hh:mm:ss).
When I want to display the data on one of my php pages, simply posting the exact field data shows the format mentioned above.
I would THINK simply using date("Y-m-d",$row["DATE"]) where $row["DATE"] corresponds to the particular row value would return the desired format.
Instead I have to use:date("Y-m-d", strtotime($row["DATE"])).
Why is this? My $row["DATE"] field is not a string in the first place. Should I be able to simple rearrange the data stored in a datetime field? Wasn't that the purpose of rebuilding my entire tableset to accomodate datetime?
MySQL has a built in function called date_format which you can use to display the date how you want to.
SELECT DATE_FORMAT(date_field, '%Y-%m-%d') as date_field FROM table_name
The manual has the list of formats and the variables needed to display it that way. Using this method there will be no need to have PHP convert it etc. Plus it is less code on PHP side for something MySQL can handle easily.
EDIT
Sorry, just read you were looking for an explanation.
PHP's date function takes in a UNIX timestamp, which MySQL is not using. MySQL uses a real date format IE: YYYY-MM-DD HH:MM:SS, as you know, this is to be compliant for years later. The UNIX timestamp has a limited range from something like 1969 to 2037 that it is valid for, which makes it really useful for "timestamping" of items such as a chat box message or items they are not expected to be around post those dates, where as the MySQL DATETIME should not die out until the year changes to 5 digits or the world ends.
Read the WIKI on UNIX timestamp for more information on it.
MySQL does allow you to select dates in unix timestamp format, which allows them to be used more easily in PHP, exactly as you requested.
The previous answer seemed to ignore this point, or downplay it due to the range restriction on the unix timestamp, but if it's what you're looking for...
SELECT UNIX_TIMESTAMP(datefield) as u_datefield FROM table
will give you the date in timestamp format, which you can use as you suggested in PHP:
<?php
$showdate = date("Y-m-d",$row['u_datefield']);
?>
As the previous answer suggests, unix timestamps do have a limited range, so if you need dates prior to 1970 or after 2038 it may not be suitable, but for everyday use today it's great.
The main advantage of using timestamps over date strings is that timestamps can be added and subtracted, which is much harder with a date in string format.