Get Date Range with Given Date - php

I have the following query:
SELECT `date_created`, COUNT(`ID`) AS no_of_registration
FROM (
SELECT `id`,
DATE_SUB(DATE_ADD(MAKEDATE(from_unixtime(`created_on`,'%Y'), 7),
INTERVAL EXTRACT(WEEK FROM from_unixtime(`created_on`,'%Y-%m-%d')) WEEK),
INTERVAL WEEKDAY(DATE_ADD(MAKEDATE(from_unixtime(`created_on`,'%Y'), 1),
INTERVAL EXTRACT(WEEK FROM from_unixtime(`created_on`,'%Y-%m-%d')) WEEK)) -1 DAY)
as `date_created`
FROM `users` WHERE 1) x
GROUP BY `date_created`
HAVING `date_created` BETWEEN CURDATE() - INTERVAL 12 WEEK AND CURDATE()
This displays the data from the last 12 weeks using the date today. However, it only shows date_created as the Monday date. I'd like to show every week as a date range. Example: 2017-11-27 - 2017-12-04.
Is this possible? I need it to display in my graph using chart.js.
Thanks for you help. -Eli

Hello not sure that i have understood your problem but if you want only the monday, you can add in your where statement :
AND DAYOFWEEK(a.Date)=2

Related

How to get records only for this week with timestamp in SQL?

I have a Table in Database which has records of Logins.
Table name: user_logins
ID | timestamp
1 2019.01.03 (Year, Month, Day)
2 2019.01.04
3 2019.01.05
4 2019.01.05
5 2019.01.07
6 2019.01.07
7 2019.01.09
I want to Show only Count of Records by this Week.
From Monday to Sunday (04-02-2019 ... 10-02-2019)
My PHP and SQL Code is:
$mo = mysql_num_rows(mysql_query('SELECT * FROM user_logins WHERE DAYNAME(DATE(timestamp)) = "monday" and timestamp >= DATE_SUB(CURDATE(), INTERVAL DAYOFWEEK(CURDATE())-0 DAY)'));
this should show the records of 04-02-2019
Here is my SQL Fiddle link:
SQL Fiddle
This:
DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)
gives this week's Monday.
So:
SELECT * FROM user_logins
WHERE
timestamp
BETWEEN DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)
and
NOW()
Try following query:
SELECT id FROM `user_logins`
WHERE timestamp >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND timestamp < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
Demo

MYSQL Last month Query is not returning first 9 days

I am querying records from the last calendar month. As it is February, it should return all the records that were added on January this year.
My Query:
`SELECT * FROM table_name WHERE date >=
DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1
DAY) AND date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1
MONTH)), INTERVAL 0 DAY) AND campaign = '$campaign' ORDER BY date
ASC`
It returns some records but skips the first 9 days. It starts showing records from 10th of the previous month. What am I missing here?
check your date field type and make sure you have not mistaken it with varchar.
SELECT * FROM table_name WHERE
(MONTH(date) = (MONTH(NOW()) - 1) AND YEAR(date) = YEAR(NOW()))
OR
(MONTH(date) = 12 AND MONTH(NOW())=1 AND YEAR(date) = (YEAR(NOW()) - 1) AND campaign = '$campaign' ORDER BY date
ASC`
Try To Get Data in step by step like,
First, you should try below query.
SELECT Date, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY) AS StartDate, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY) AS EndDate FROM MyTable
Second, If First Step give right date then get your data by directly writing your date rather than DATE_ADD function in where clause.
Third, If These will Give you write DATA then try to fetch data using DATE_ADD function.
Replay If you will get solution.
SELECT * FROM table_name WHERE date between DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 MONTH) AND DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 DAY) AND campaign = '$campaign' ORDER BY date ASC

What should be MySQL query for the following?

I wish to show upcoming birthday records on dashboard. I have MySQL table 'contacts' which
has field dob (DATE YYYY-MM-DD). I want to retrieve all the records with birthday is coming
in next 15 days. e.g is: birthday is 1986-03-24 it should be in result.
Take a look at MySQL DATE_ADD and INTERVAL.
By using both as below you can get record that are coming in next 15 days.
SELECT * FROM user WHERE dob BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 15 DAY)
FOR birthrate comparison.
MAKEDATE(YEAR(NOW() + INTERVAL 15 DAY), DAYOFYEAR(birthday)) BETWEEN CURDATE() AND CURDATE() + INTERVAL 15 DAY
SQLFiddle Demo
You have to use the date in mysql query as:
SELECT FROM table_name WHERE timestamp_row BETWEEN
'2011-01-01 00:00:00' AND '2011-01-02 23:59:59';
You are doing the B'Day dates of users and thus hope this would be do the trick of formatting and selection of date
select id from my_table
where
timestamp < date_format(date_add(CURRENT_TIMESTAMP(), interval 1 day),
'%Y%m%d000000' ) AND timestamp >= date_format(CURRENT_TIMESTAMP(),
'%Y%m%d000000')
Can do by this way also
SELECT * FROM person WHERE IF ( MONTH( NOW() ) < 12,
MONTH( birthdate ) = MONTH( NOW() ) + 1, MONTH( birthdate ) = 1)
You can use this query. Try Out
SELECT * FROM TableName
WHERE REPLACE(BirthDate, DATEPART(YEAR, BirthDate), DATEPART(YEAR, GETDATE())) BETWEEN GETDATE() AND DATEADD(DAY, 15, GETDATE())

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.

PHP mysql_query - Get row of current week with current date

I have a mysql table as follows:
week start_date end date
1 2011-04-25 2011-05-01
2 2011-05-02 2011-05-08
3 2011-05-09 2011-05-15
I would like to run a query to get the week number when the current date is between the start_date and end_date of a specified week.
SELECT week
FROM table_name
WHERE CURRENT_DATE() BETWEEN start_date AND end_date
CURRENT_DATE() is synonyms for CURDATE().
SELECT week FROM TABLE_NAME WHERE CURDATE() BETWEEN start_date AND end_date;
Pretty simple:
SELECT week FROM my_table WHERE CURRENT_DATE > start_date AND CURRENT_DATE <= end_date

Categories