Month and Year with PHP and MySQL? - php

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);

Related

How insert only year in mysql date column

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

How to select data based on months using mysql

I'm a beginner in php and making a small online banking system in php for my College project. I wanted to generate the statement for each month. I'm recording each transaction happening in each days. I wanted to retrieve rows from the table till the end of each month beginning from the starting of the account. So If I click on the view statement link, (considering that I opened the account on 14th of Feb) I need to see two statements. 1 from 14th of Feb to 28th of Feb and another one from 1st of March to current date.
What mysql query should I used to get this?
You might try appending to your current query the condition for the current month, you can -1, -2, -3, etc... to the AND MONTH... condition.
AND
YEAR(`date_column`) = YEAR(NOW())
AND MONTH(`date_column`) = MONTH(NOW())
where date_column is the name of the column you are working. It might also be that you are trying to GROUP BYMONTH(date_column)` its a bit hard to tell.

Display Selected Month Values in mysql PHP

In MYSQL database, I have the date column which contains date in format [29-11-2012].
I would like to display the values based upon month selected by user via php page.
For Example, If user selects OCT then this should display all the other columns for this particular month.
Can you suggest SQL Query for this?
Thanks,
Shail
You can use
WHERE MONTH(STR_TO_DATE(formdate,'%d-%m-%Y'))
like this condition
Basically mysql stores dates in yyyy-mm-dd format only..

How to sort data depending on what day it was posted?

How do you sort data which was stored in a mysql database depending on the days of the week in which the data was submited ??
I basically want to create a diary which outputs information in each day of the week depending on what day it was posted by dates so,
Mon - Data in order of date
Tue -
Wed - e.t.c
Any code examples and information will be great, thanks.
You can do a
SELECT DAYOFWEEK(datehere) as dayofweek, datehere FROM something ORDER BY dayofweek, datehere;
You can use the DAYOFWEEK function to extract the day, and then sort on it just like any other data.
What kinf of data type is the column where you store the date submission?
It seems like you're asking for a basic SELECT statement?
SELECT some_column, another_colum FROM your_table ORDER BY your_date_column DESC
This assumes you actually have a column that logs the insertion timestamp.
If this answer is obnoxiously simplistic, please forgive me...and give us more details :)
Regards.
If your data is stored as a DATE or DATETIME field, use the DAYOFWEEK or DATE_FORMAT functions to turn it into day name for output, but continue to order by the DATE field
SELECT DATE_FORMAT(my_date_column, '%W') AS dayofweek
FROM my_table
ORDER BY my_date_column
Well, the sorting bit is easy, just sort on the column that represents the post's date. The grouping in days is something you can do in your code, since you need to check the date there anyway (for post-processing the actual output).
To put it this way, you can do a subselect to get the specific day of the week, but in your code you would have to check the day again to group posts per day. In that case it's better (and cleaner, since you're separating business logic from data) to do this in your code, something like this:
select all posts (within date range)
make an associative array, with the
days as keys, and posts (in new
arrays) as values
loop through the
days and then posts to output the
posts per day
SELECT *
FROM diary_entries
ORDER BY FIELD(DAYOFWEEK(created), '2, 3, 4, 5, 6, 7, 1'), created
DAYOFWEEK grabs day of the week number (1 = Sunday).
FIELD makes Monday first day of the week.
After sorting by day of week, then sorted by date created.

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