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

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.

Related

best date time mysql format for jalali calendar [duplicate]

I have a mysql table that has a column with date type.
I'm going to store non-Gregorian date in this column (i.e. Jalali date).
I tested this by using phpMyadmin and storing a Jalali date and no error happend.
Is it a good idea to store non-Gregorian dates in date type?
If not; which is better? storing it as varchar or as timestamp or timestamp as Int or something else?
Is it a good idea to store non-Gregorian dates in date type?
No. Aside that some valid date in one calendar system doesn't exist in another calendar, functions working on DATE typed columns may not work properly. The matter is not just storing data, you need to process this data and for example compare them with CURDATE().
storing it as varchar or as timestamp or timestamp as Int or something else?
If you choose a proper formatting, use two digits for month and day and static number of digits for year, a character string type, CHAR or VARCHAR is fine. Comparing theme against each other is just a lexical comparison and you still can write your functions o procedures to extend functionality.
Choosing TIMESTAMP or DATE changes the question as former represents a specific time but latter represents a specific entry in calendar. If you want put time beside date they still differ in meaning. You should think about issues like daylight-saving time changes which cause some people prefer to put calendar entry (DATE) and some prefer seconds passed from 1 Jan 1970 (TIMESTAMP). e.g. there is two timestamps for 1393-06-30 23:30:00 in Hijri Shamsi calendar based on current Iran government laws.
You can store non-gregorian dates in an integer field in the database.
Examples:
year:1396, month:11, day:17 => 13961117‍‍‍‍‍
year:1393, month:4, day:9 => 13930409
by using this, you can query rows to find a specific date and dates that are <=> than a specific date, but unfortunately, you can't compare them against each other.
Internal storage and binary interface of almost all database systems have nothing to do with calendars. you just store date (and possibly time) into database, providing no calendaring information. It's only a simple number of days, seconds or milliseconds past from a specific point in time (usually midnight 1970-01-01).
So you just need to provide an API abstraction of dates in your application layer. Everything else will work. You convert all your dates to Gregorian or Unix timestamp, and send queries to MySQL as usual.
Non Gregorian calendar still operate by Year, Month, Day and Time so it can be stored in 4 separate columns like:
CREATE TABLE `Calendar` (
`Year` int,
`Month` tinyint,
`Day` tinyint,
`Time` time
);
I make possible to store dates without conversion and permit group by Year and Month

create time stamp and compare time by mysql field [duplicate]

This question already has answers here:
MySQL convert date string to Unix timestamp
(4 answers)
Closed 8 years ago.
there is a table with these fields
month
year
day
and these files are contain numbers like
year = 2001 and month = 10 and day = 25
and i have time stamp in php
so i need to compare these in mysql
something like:
select * from times where mktime(year,month,day)<1235478554
is there any function in mysql for this?
i need to make time stamp in mysql with the mysql fields...
UPDATE 1 :
i used like this and not worked
SELECT * from work_result where UNIX_TIMESTAMP('year-month-day 00:00:00')<1
UPDATE2:
There's UNIX_TIMESTAMP which will get you the Unix timestamp of a given date string. Which means you need to build a date string inline in your MySQL query. Doing so is considered bad style, you should propably be storing a Unix timestamp within your database.

Date Time or Time Stamp? [duplicate]

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

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;

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