MySQL - Between months (rather than a timestamp) - php

how would I generate a MySQL query to grab a row that is between a particular month
eg grab row where month is between 01/01 (1st Jan) and 01/06 (1st June)

I'm not sure, if you want to grab all rows from Jan to June, only the rows until June 1st or all rows from Jan to May. You can use for example:
[...] WHERE `date_column` BETWEEN '2012-01-01' AND '2012-06-01'
which gives you all rows including June 1st.
Use this to get all rows from the full months of Jan to June:
[...] WHERE YEAR(`date_column`)=2012 AND MONTH(`date_column`) BETWEEN 1 AND 6
(change to BETWEEN 1 AND 5 for Jan to May)

If you generate your SQL query in PHP, you can use BETWEEN
$date = date("Y") . '-' . date("m") . '-01';
$sql = "SELECT *
FROM `table`
WHERE `date_column` BETWEEN '$date' AND '$date' + INTERVAL 1 MONTH - INTERVAL 1 DAY";
(I subtracted one day to exclude the data of the next month first day)
Or you can use EXTRACT
$month = 5;
$sql = 'SELECT *
FROM `table`
WHERE EXTRACT(MONTH FROM `date_column`) = ' . $month;

Assuming the column in mysql is the right format (date/datetime) you can simply compare (>,<) :
SELECT somecolumn
FROM sometable
WHERE datecolumn >= '2012-01-01' AND datecolumn < '2012-06-01'
Hope it helps!

You can use the month(), year() and day() functions. So your query would look like something like
SELECT * FROM table WHERE month(date_field) >= 1 AND month(date_field) <= 6 AND year(date_field) = 2011
date_field should be a date or datetime field, if it is a timestamp, you need to use the FROM_UNIXTIMESTAMP function so it would look like month(FROM_UNIXTIMESTAMP(timestamp_field))

SELECT
*
FROM
yourtable
WHERE
date BETWEEN '2012-01-01' AND '2012-06-01';

Related

Convert integer to date in mysql ,php

I have 2 columns one having month and another year , I want to get all the records greater than that month and year
I cannot use date functions as those columns are of integer type, It gives incorrect results
How can I get records greater than that month and year
For example How can I get events which took place after february 2017
What about this approach:
SELECT * FROM table WHERE CONCAT(year, month) > 201702
WHERE CONCAT(year, month) > 201702
probably you have also some php code inside so:
$date = strtotime('2018-01-01 01:01:01');
$sql = 'SELECT * FROM YOUR_TABLE
WHERE CONCAT(year, month) > ' . date('Ym', $date);
SELECT * FROM events WHERE CONCAT(year,month) >= 201702
or without CONCAT
SELECT * FROM events WHERE( month > 2 AND year =2017) OR (year > 2017)

Filter Dates in sql by months - php,sql

I have a table in sql and in that table I have column with the action date by this format: 2015-08-26 05:54:39
Now I built a system in php and i want to get all the actions that was in the same month.
I try to run that:
SELECT * FROM `Expenses` WHERE DateAction between '2015-08-01 00:00:00' and '2015-09-01 00:00:00'
and its return the actions dates that was in the same month, But its can return the the action that was in 2015-09-01 00:00:00 so i understand i need to get the last day in the month and put it in the sql query.
How I do that?
Right now this is the call to the sql:
$query = mysql_query("SELECT * FROM `Expenses` WHERE `accountid` = $accountId");
Fortunately for you, there's MySQL's MONTH() command.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_month
SELECT * FROM `Expenses` WHERE MONTH(DateAction) = 8;
//Selects all rows in the month "August"
All months are recorded numerically (1 is January, 2 is February,...... 12 is December). It will always be precise based on your database time and date settings.
If you want to customize your query further (selecting the same year and month), then you can read more about date commands here
SELECT * FROM `Expenses` WHERE Year(DateAction) = '2015' AND MONTH(DateAction) = '8';
//Selects all rows in year 2015 and month "August"
you could use YEAR() and MONTH() or as simple as:
WHERE
DateAction >= '2015-08-01 00:00:00'
and
DateAction < '2015-09-01 00:00:00'
This will not include the september date.
I think it may help you
Suppose if you want the result or records of August, you may user the query
SELECT * FROM Expenses WHERE DATE_FORMAT(DateAction,'%c')='8'

Show today orders from table

I do not understand how to get output from my sql table .
the table date is stored in int . for example date output from one row is like 1362157869 .
i want to show today orders in query :
php : $today = date("y-m-d", time());
query : SELECT * FROM test WHERE date = '$today'
but it didn't work . i also try this :
SELECT * FROM test WHERE date LIKE '$today'
Query without PHP var:
SELECT * FROM test WHERE date = DATE(NOW());
You are storing your date as a UNIXTIMESTAMP, so you have to convert it to DATETIME using FROM_UNIXTIME, and then you have to extract only the date part, ignoring the time, using the DATE() function:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = CURDATE()
Please see fiddle here. You can then extract yesterday records with something like this:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = CURDATE() - INTERVAL 1 DAY
Or a specific date with this:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = '2013-03-01'
To make use of an index
Assiming that your date column could contain not only the date part but also the time, you could also use this that will return all today records:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP(CURDATE())
and this for yesterday:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP(CURDATE() - INTERVAL 1 DAY)
AND date < UNIX_TIMESTAMP(CURDATE())
or this for a specific date:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP('2013-03-01')
AND date < UNIX_TIMESTAMP('2013-03-01' + INTERVAL 1 DAY)

what where clause should I use to get objects greater than the creation date?

In my DB the objects are saved with creation date in the format, dd-month-year hours:seconds (example 2011-12-07 09:59:41). What sql commands should I use to get objects created after April 2012?
-- After April 1st, including
SELECT *
FROM mytable
WHERE creationDate >= CAST('2012-04-01' AS DATE)
-- After April 1st, excluding
SELECT *
FROM mytable
WHERE creationDate >= '2012-04-01' + INTERVAL 1 DAY
-- After April
SELECT *
FROM mytable
WHERE creationDate >= '2012-04-01' + INTERVAL 1 MONTH
SELECT * FROM mytable WHERE CreationDate > '2012-04-00'
WHERE DATE_FORMAT(creationDate, '%d/%m/%Y') >= STR_TO_DATE('01/05/2012', '%d/%m/%Y')
(you are asking after april 2012, which is may)
Try this link
You want the bit at the bottom.
Example:
SELECT * from table where DATE >='2008-12-03';

HOw to SELECT * (all) which date start from the last day of last month to the first day of next month

use PHP and MySQL and want to use SELECT statement which date_post(datetime variable) start at the last date of last month and to date the first day of next month, help me please.
Thank you in advance.
my database: 'id', 'content', 'image', 'date_post',
etc. and I try to use
$today = getdate();
$thisyear=$today['year'];
$thismon=$today['mon'];
$date_start=$thisyear.'-'.$thismon.'-01';
$date_end=$thisyear.'-'.($thismon+1).'-01';
$sql="SELECT *, DATE_FORMAT(date_post, '%d-%m-%Y') AS datepost
FROM my_table
WHERE date_post BETWEEN date('$date_start')
AND date('$date_end')
ORDER BY date_post DESC";
It makes with one query in MySQL, without any PHP:
SELECT * FROM `table_name`
WHERE DATE(`date_post`) >= DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH, CONCAT('%Y-%m-', DAY(LAST_DAY(CURDATE() - INTERVAL 1 MONTH))))
AND DATE(`date_post`) <= DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH, '%Y-%m-01');
Ensuring that the query will not scan the full table but will use the index of date_post (if there is one!):
SELECT *
FROM myTable
WHERE date_post < LAST_DAY(CURDATE())
+ INTERVAL 2 DAY
AND date_Post >= LAST_DAY( LAST_DAY( CURDATE()) - INTERVAL 1 MONTH )
If it is run today ( 2011-07-01 ), it will give all datetimes between 2011-06-31 00:00:00 and 2011-08-01 23:59:59.

Categories