How insert only year in mysql date column - php

I have a date that sometimes can be only a year, years and month or a full date. What is the best practice to save it on the db?
I need to do search later as: Give me all date between 2004 - 2008 or give me all date between 2004-05-21 to 2005-12-31

I would suggest to use DATE format (https://dev.mysql.com/doc/refman/5.5/en/datetime.html) and then if needed use more specific functions to query records by date ranges like
WHERE YEAR(created) BETWEEN 2010 AND 2011
etc.
More date\time related functions here:
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

Related

PHP SQL Query to compare DATE from predefined datetime format

My datetime format on my SQL Table looks like this
Table1:
Col1
FromDate
--------------------------
10 August 2017 - 02:10 pm
Col2
ToDate
--------------------------
10 August 2017 - 08:00 pm
What would be the applicable SQL SELECT query and or php code to check if ..
$submitFrdate = "10 August 2017 - 3:00PM";
$submitTodate = "10 August 2017 - 6:00PM";
will be compared to the table using it's DATE only if any of the dates from either the first or second variable collides to the dates of any of the 2 table columns? I understand that I can trim the time stamp using substr() to make it into just 10 August 2017 but the data on the table includes the time. Other than that, I am not sure whats the easiest way to make sure either of the submitted data doesn't hit either of the column's dates REGARDLESS of the time.
Thanks in advance.
MySQL has a function called STR_TO_DATE() which can reverse-format a date string into MySQL's native date format. Then you can do comparisons in the SQL.
Perhaps better solution is to use the DATETIME type for the column. Then you can directly do comparisons with them.

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.

Fixing old MySQL date field mistake

I've have a date field in my table that stores dates from a form in a Weekday, Day Month, Year string.
Example: Wednesday, 02, November 2010
the field is also a varchar.
can I loop through the entire table with a php script to convert the dates to a mysql date format or perform this with an SQL query?
I'm not sure that I can preform some certain statistical reports that involve picking out certain dates and date ranges in the format I have now. What are my options?
No need to get PHP involved. It can be done directly in MySQL:
ALTER TABLE yourtable ADD fixeddate date;
UPDATE yourtable SET fixeddate=STR_TO_DATE(bad_date_field, '%W, %d, %M %Y');
relevant docs here: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
PHP has a built in class called DateTime. Here is a snippet of code that should get you started.
$date = DateTime::createFromFormat('l, d, F Y', 'Wednesday, 02, November 2010');
echo $date->format('Y-m-d');
You can find more info about this class at http://au.php.net/manual/en/book.datetime.php

Month and Year with PHP and MySQL?

I need to add a column to my MySQL database for the month and year. For example, when inserting an entry, it can either be December 2010, April 2009, May 2011, etc.
Can someone provide me with some SQL code that I can use to insert this into my database? I'm confused as to whether I should store it as 2011-07-30, a Unix timestamp, etc.
I also need to do the following:
Drop down for the month/year (going back 5 years and in advance 5)
Display the selected month/year when editing entries
Be able to get results when doing searches like December 2010
If you could also provide examples of how to do the items above that would be excellent.
for month and year you can add following query in your database string...
select to_char(date_column_name, 'MONTH YYYY') from table_name
click here for more information
for drop down you can use following condition over there
where to_number(to_char(date_column_name,'YYYY'))
>= to_number(to_char(curdate,'YYYY') ) -5
click here for more information
•Be able to get results when doing searches like December 2010
for this last option you have to pass this text box value to database and want to put this value in where condition like below-
select * from table_name
where to_char(date_column_name,'MONTH YYYY') = front_end_object_name
You should insert timestanp so that you can directly convert it in to whatever format of date you want.
//December 2010
date('F Y',$timestamp);

Archive Builder PHP

Before i start id like to say ive posted this question as more of a discussion rather than Problem Question.
In my Database i have news posts lets say with 3 columns (Id, title, date). Wher Id and title are self Explanitory the date is stored in mktime() values, in other words the number of seconds passed since 1 January 1970.
Now what i want to do is build an archive link that will display as such
July 2009
June 2009
March 2009
Feburary 2009
December 2008
Note the months on which there were no posts are not displayed.
Now as an initial thought i was thinking
Start with the last day of the current Month
And get the Value of the First day of the current Month
Do a MySQL COUNT Query/mysql_num_rows for posts that were date >= First_Day_Seconds AND date <= Last_Day_Seconds
Display or put the values in an Array
Do another Query to Check if Any more values are found WHERE date < First_Day_Seconds (break if no rows were found)
Now the above is just something on the top of my head. But if you got any ideas to speed this process up please share.
Will say in advance, date needs to be in mktime format
I would suggest using a database "native" time format, but it works with UNIX timestamps as well.
You can simply do:
SELECT DISTINCT FROM_UNIXTIME(date, '%M %Y') FROM posts;
Optionally with a WHERE clause limiting the dates to past or future dates. Possibly an ORDER clause thrown in for good measure. That should be pretty much all that's needed, let the database do as much work as possible.
If you need more formatting options, select the dates with "%Y-%m" instead and format them in PHP:
date($myCustomFormat, strtotime("$date-01"));
You can use this query to get years
"SELECT *,content_id,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload,'%Y') DESC";
now use can use this query to get month of that year
$tday = date("Y", $datetime);
$s1="select * from content_mast where DATE_FORMAT(date_upload,'%Y')=$tday";

Categories