PHP mysql_query - Get row of current week with current date - php

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

Related

How to select from MYSQL database WHERE its only from two DATETIME?

I have a tabel in MYSQL database that has 2 columns.
1- start_date
2- end_date
the above columns both hold DATETIME like so:
start_date end_date
2019-10-03 13:30:00 2020-10-03 23:30:00
I need to select from the MYSQL table ONLY if the current date is between the start_date and end_date.
So i tried this MYSQL Query:
SELECT * FROM table WHERE `end_date` >= CURDATE() AND `start_date` <= CURDATE()
but this doesn't work and doesn't return anything from the database even thogh the the current date is between the start_date and end_date.
Could someone please advice on this?
Q: I need to select from the MYSQL table ONLY if the current date is between the start_date and end_date.
Try this.
SELECT * FROM `table` WHERE CURDATE() between start_date and end_date
Convert it with date before because you're using their timestamp.
SELECT * FROM `table` WHERE CURDATE() between date(start_date) and date(end_date)
Or
SELECT * FROM `table` WHERE NOW() between start_date and end_date
Using SQL date function in query
SELECT * FROM user WHERE date(end_date) >= CURDATE() AND date(start_date) <= CURDATE()
try this
SELECT * FROM table
WHERE start_date >= '2019-10-03 13:30:00'
AND end_date <= '2020-10-03 23:30:00'

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

mysql query get 1st specific day between 2 dates

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

retrieve data from sql where start date is equal and between end date

i got a table that stores start_date and end_date with the field type DATE
I want to retrieve all records between the start_date and end_date but my below SQL statement do not give any results.
$sql = "SELECT * FROM expo Where date >= DATE(NOW()) AND end_date <= DATE(NOW())";
You can use the between function to perform this action.
SELECT *
FROM expo
WHERE NOW() BETWEEN date AND end_date
Oh, since your column is date, not datetime use date(now()). That will select records that start and end on the current date as well.
Demo: http://sqlfiddle.com/#!9/0aaad/6
SELECT *
FROM expo
WHERE date(NOW()) BETWEEN date AND end_date
Use curdate
SELECT *
FROM expo
WHERE date >= curdate() AND end_date <= curdate()

Categories