So I have a table ("posts") where date is formatted as such (PHP syntax)
date = date("F j, Y")
and this is the date submitted in the posts table as well. How would I get all posts where date is younger than 7 days (i.e. current_date - 7) via an SQL query?
Any help appreciated
You need to convert that date string to an actual date MySQL can work with. You can use STR_TO_DATE() for that.
SELECT *
FROM posts
WHERE STR_TO_DATE(date_col, '%M %e, %Y') > CURRENT_DATE - INTERVAL 7 DAY
or
SELECT *
FROM posts
WHERE STR_TO_DATE(date_col, '%M %e, %Y') > DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)
Related
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)
How do I get the first Sunday between 2 dates?
i have only 2 fields in myTable (dt_from and dt_to)
dt_from = '2016-08-6';
dt_to = '2016-08-19';
SELECT firstsunday FROM myTable BETWEEN dt_from AND dt_to;
How about getting the first Sunday just after from date itself?
SELECT DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) from myTable as A;
http://sqlfiddle.com/#!9/7fce5
If Sunday isn't between the dates:
SELECT DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) as firstsunday from myTable as A where DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) between A.date_from and A.date_to;
http://sqlfiddle.com/#!9/83d0f0/4
A little bit shorter:
SELECT firstsunday from (SELECT A.date_from, A.date_to, DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) as firstsunday from myTable as A) as B where B.firstsunday between B.date_from and B.date_to;
http://sqlfiddle.com/#!9/6adab3/1
Try this:
SELECT dt, DAYNAME(dt) as day
FROM myTable WHERE dt BETWEEN date1 AND date2
ORDER BY dt
WHERE day = "Sunday"
LIMIT 0,1
assume you only need to sue the db to get the date range so you dont want any king of query.
<?php
echo date('m/d/Y', strtotime('next Sunday', strtotime('2016-08-6')));
you can add some validation to make sure its before your end date
This question already has an answer here:
From the timestamp in SQL, selecting records from today, yesterday, this week, this month and between two dates php mysql
(1 answer)
Closed 8 years ago.
I want to show records of today (from yesterday 12 AM to today 11:59 PM), Yesterday, and records of this week,
I have this query for todays records
SELECT COUNT(*) FROM `tblpatients` WHERE `Is_Deleted` = '0' AND `TimeStamp` <= NOW() AND `TimeStamp` >= ?????
I have a field in my table named TimeStamp format is 2014-09-20 12:11:20
Make your calculations based on CURDATE if you are selecting date only.
Hope the below Examples would help you
Today: WHERE timestamp >= CURDATE()
Yesterday: WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND timestamp < CURDATE()
This month: WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL DAYOFMONTH(CURDATE())-1 DAY)
Between the two dates 3 June 2013 and 7 June 2013 (note how the end date is specified as 8 June, not 7 June): WHERE timestamp >= '2013-06-03' AND timestamp < '2013-06-08'
Please see this one too..
Use strtotime() and date() function for getting yesterday. Example....
$now = date('Y-m-d h:i:s', time());
$yesterday = date('Y-m-d h:i:s', strtotime('yesterday'));
$sql = "SELECT COUNT(*) FROM `tblpatients` WHERE `Is_Deleted` = '0' AND `TimeStamp` <= '$now' AND `TimeStamp` >= '$yesterday'";
Also can use BETWEEN in query String. ie,.
$sql = "SELECT * FROM tblpatients WHERE Is_Deleted = '0' AND TimeStamp BETWEEN '$now' AND '$yesterday'";
The code below is to select and then echo stuff from my db grouped by either weeks or months. I have a couple of more of these and then use offset to get the second week etc.
FROM my_db WHERE DATE >= CURDATE() - INTERVAL 10 ".$grouping1." GROUP BY ".$grouping."(DATE) order by DATE desc limit 1
I have two questions:
I would like to exchange curdate() with a variable instead so that we are able to "pick a startdate" and then look 10 weeks/months back from that date.
Is there a simpler/better way to do this?
I've tried to exchange the curdate with a variable like this:
FROM my_db WHERE DATE >= '". $startdate ."' - INTERVAL 10 ".$grouping1." GROUP BY ".$grouping."(DATE) order by DATE desc limit 1
but the weeks/months stay the same even though the variable (start date) changes.
Edit: My $startdate variable is formatted like this 2013-05-07 and the DATE column in the mysql db is of date format.
Peace
/Adis
Something like:
FROM my_db WHERE DATE >= '".date(Y-m-d, strtotime($my_date))."'
Will work
Try this.
This will subtract 10 months from current date
FROM my_db WHERE DATE >= '".date("Y-m-d", strtotime("-10 months"))."'
This will subtract 10 weeks from current date
FROM my_db WHERE DATE >= '".date("Y-m-d", strtotime("-10 weeks"))."'
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.