Date Time or Time Stamp? [duplicate] - php

This question already has answers here:
Should I use the datetime or timestamp data type in MySQL?
(40 answers)
Closed 9 years ago.
I am creating a table which has create_date and modify_date field in mysql, but I am struggling in decide which data type is the best choice for these 2 fields. Should i use date time for both fields, or should i use date time for create_date and time stamp for modify_date ?

Depends on your requirements. If you want to know the exact time these things happened then use a date/time (DATETIME in MySQL). Or, if you're happy with with just the date then choose date (DATE in MySQL)! If you don't know when maybe use DATETIME just to be safe (so you have the precision if you need it).
You're using MySQL so here is the documentation: http://dev.mysql.com/doc/refman/5.0/en/datetime.html

I'm assuming you're talking about a database table. If that is the case and you need to know date information choose a date time for both modify_date and create_date. If you don't need date information use a time stamp.

Sorry I'm from Ukraine and I use Google Translate. It all depends on how you are going to use them, if you take away from each other then TimeStamp, if just to show (for example in the news), then it is better Date Time. Generally I recommend TimeStamp

Related

What should i use to save date and time [duplicate]

This question already has answers here:
Should I use the datetime or timestamp data type in MySQL?
(40 answers)
Closed 8 years ago.
For my new database i want to save a given date and time and year from php to my database with mysqli, in sql i can make a field :
DATE
DATETIME
TIMESTAMP
TIME
YEAR
I'm using the database only with php, what type should i select? and for what reason.
I think datetime and timestamp are the best options. but cant find any reason why 1 should be better then the other. Can someone help me to chose ?
Or is it better to save date time and year separate?
I want to make querys to get values from last week etc.
For my new database i want to save a given date and time and year from php to my database
So, store it as DATETIME. It includes the year.
Here is how look at those types:
DATE: use for dates (with years), for example a a birthdate ("2010-11-23")
TIME: use for a time in a day, for example the start of your lunch ("12:00")
DATETIME: use for a specific date and time, for example the start of a meeting ("2010-11-23 12:00")
TIMESTAMP: use for when a specific thing happened, for example the time a certain meeting was created ("1385653500"; this often includes timezone information in its definition)
YEAR: use to store a year, for example the start year of a war ("1653")
Note that you can always cast "larger" types to "smaller" types. E.g. you can cast a DATE into a YEAR.

What's the best way to store date and time in a MySQL database for later display with PHP?

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.

Add 1 hour to stored date / time value [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Add 1 hour to datetime datatype
I have a timestamp stored in mysql db that I need to add 1 hour to before I display in our application (the timestamp is based on Central time, and I want to show Eastern). Quite simply I just need to add 1 hour to the value.
The stamp is stored as:
2012-02-12 05:20:03
Alternatively, once I retrieve it from the source, I could add an hour before storing it. I store it using the following foreach:
$created_at = strtotime($item->created_at);
Suggestions are greatly appreciated.
Instead of storing the formatted time, store the timestamp, either using the PHP time function or the mysql timestamp,
Then convert that back into a formatted time based on your needs using the php date function in this case or whatever other date formatting functions exist for other languages, except before you format the dates, add 3600 seconds/1 hr to them in order to achieve your objective.
Hope that helps.
May I suggest you use the CONVERT_TZ() function in MySQL? You just specify the original TZ and the TZ you want to convert to in the query, and voila, it's converted.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_convert-tz
You wouldn't need to worry about messing with the time functions in PHP.
Update tableA
set created_time= DATE_ADD(created_time, INTERVAL 1 HOUR) where unique_id =4;

Which is the better way of storing date and time in MySQL?

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

Unix timestamp vs datetime [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Datetime vs Timestamp?
I have a Mysql table, which has a column add_date. It tracks the date/time when the record was added to the database.
Queries based on this table:
Display when the record was added in the format: 2 hours ago, 4 weeks ago, 1 year ago etc
Allow users to search records inserted in any day/month/year. So the user may be allowed to choose to see the records inserted in 2009 only.
Which would be better in this case - unix timestamp or datetime?
Right now I'm using both, but since the table will have millions of records over time, having both columns may affect the size of the database.
Unix timestamp seem to be better for conversion to 2 hours ago format in PHP and also it is timezone independent. But datetime has better readability and making a query for a particular date/time/year seems easier.
Your suggestion?
When you have the choice, I'd say go for mySQL dates.
You won't have to take care of date range issues
You can easily query time spans using mySQL's date functions (BETWEEN(), DATE_ADD etc.)
Date related queries will be much faster, especially when you have millions of records, because you won't have to use FROM_UNIXTIME() which can be expensive in large queries
It's child's play to convert DATE fields into UNIX timestamps when necessary.
I'd go for the mysql formats, just because mysql has a plenty of datetime function, to use which with timestamp will cost you another conversion.
but since the table will have millions
of records over time, having both
columns may affect the size of the
database.
oh my.
are you really concerned in 4 additional megabytes of space?
You can get the best of both worlds by using a unix timestamp and the MySQL from_unixtime() function to convert the result to datetime format where needed.

Categories