Get Current year and next form mysql - php

Im am selecting various things from a table. The problem is I only want to select them if they belong to the current year and the next year.
My table looks like this
Title Text Date
The date is formated like this 0000-00-00 and is in the format "date"
So the question is how can i select only items from only this year and the next?
Example: the year is 2012, I have items in my table that is old and i dont want them to show - I only want to select items from at the first 2012 1 January and last in this case 31 Dec 2013 current year 2012 + 1 year.
Thanks a lot for any help!

SELECT
*
FROM
table
WHERE
YEAR(date) = YEAR(CURDATE())
OR
YEAR(date) = YEAR(CURDATE()) + 1
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

SELECT
*
FROM
table
WHERE
date BETWEEN CONCAT(YEAR(CURDATE()),'-01-01') AND CONCAT(YEAR(CURDATE())+1,'-12-31')
As ugly as it looks, it allows your query to use index on date field.
Better idea is to create limiting dates in external PHP script, so that the query can use query cache.
If you only want to show items, that are no older than two years, you can do this:
SELECT
*
FROM
table
WHERE
date >= CURDATE() - INTERVAL 2 YEAR;

Related

Get Year from complete data filter year

I have a data in my db, like:
2016-05-17 , 2015-05-20 , 2015-02-02
I want to get only the years to create a list, like:
2016 , 2015
I'm making this:
SELECT * FROM insercao GROUP BY SUBSTR(data,0,4)
But this return only:
2015
How can I return All not duplicated years?
select distinct EXTRACT(YEAR FROM date_column_name) FROM date_extract;
The extract keyword of MYSQL extracts years form the date format that you mentioned in question.
Distinct will only select non repeating years.
If data is a datetime field, you ahve to change your query in this way:
SELECT * FROM insercao GROUP BY YEAR(`data`)
Edit:
In fact, to return only year value, you have to act in this way:
SELECT YEAR(`time`) as year FROM insercao GROUP BY year

How to Delete MSSQL Table Rows if they are Older than One Year/One Day

I am trying to create a PHP query to delete SQL table rows if the order_date column is a over a year old. I would also like a second query where table rows will be deleted if the same column is a day old.
To go more in depth, the order_date column is in my current table, tracking_orders, and was created using part of a query: order_date datetime NOT NULL DEFAULT GETDATE(),. The tracking_orders table currently looks like this:
order_date tracking_order account_id
Apr 7 2014 3:49PM 1 1
Apr 7 2014 3:51PM 2 1
The day I am asking this question is April 8th 2014, so I am trying to make a query to delete rows if the order_date data is Apr 7 2013, and another query to delete rows if the
order_date data is Apr 7 2014. Also, if the data is more than 1 year old, I would still like it to delete. For example, Apr 7 2012.
For a code example, I am looking for something like this:
$sql = 'DELETE * from tracking_orders WHERE order_date == DATE_SUB(getdate(), INTERVAL 1 YEAR)';
And something like this, to delete after one day:
$sql = 'DELETE * from tracking_orders WHERE order_date == DATE_SUB(getdate(), INTERVAL 1 DAY)';
Note: the above doesn't work because DATE_SUB is MySQL syntax, and I am working with MSSQL.
Thank you for any help. All help is greatly appreciated.
Try this:
DELETE from tracking_orders
WHERE DATEDIFF(dy,order_date, getdate()) = 1
OR DATEDIFF(yy,order_date, getdate()) > 1
The first condition in where checks for a difference of 1 day, and the second for a difference greater than 1 year. Note that this is going strictly by the calendar so the below will also return 1 even though time difference is less than 24 hours:
select datediff (dy, '2013-01-01 23:00:00.000', '2013-01-02 22:00:00.000')
To check for 24 hour difference only use DATEDIFF(hh,order_date, getdate()) = 1

How to get year just before current year from MySQL table

I need a record whose year is just before the current year. My table schema and data is like this
aca_id|aca_start_date | aca_end_date
------|---------------|--------------
1 |2013-04-01 |2014-03-31
3 |2015-04-01 |2016-03-31
7 |2014-04-01 |2015-03-31
I want to fetch the record depending on the current year condition is - if current date year is 2014 then fetch the 1st record because its aca_start_date is 2013 which just before 2014. If current date year is 2015 then fetch the record 2nd record. I cannot sort the table on aca_start_date because table is sorted on aca_id in descending order for other purpose.
Need guidance how can I do this job in MySQL query or if this is possible to do by using php
Here is one way:
select t.*
from table t
where year(aca_start_date) = year(now()) - 1
limit 1;

PHP/MySQL targeting only month from date field

My table has a date column formatted as yyyy-mm-dd.
Is it possible to have a query that only lists results found for any day in a specific month? There could be any month of any year in this table, but say i want to list them for only june 2013?
Thanks
SELECT * FROM tablename WHERE date_column_name LIKE '2013-06%'
select * from your_table
where date_column between '2013-06-01' and '2013-06-30'

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