PHP Count Between Date and Todays Date - php

In my dates database I have a table of dates that have two followups to be completed, one after 30 days, one after 60 days. I need to build a page that uses the MySQL query to pull all dates from the dates table that have a 30day value of No (which I can do). Now the tricky part is, I need it to only output the dates that meet that criteria, and are 30 days from the current date.
For example: August 4 & 6 have a 30day value of No, August 5 has a 30day value of Yes. Today's date is September 4. 30-days prior would be August 5.
I need the query to only display August 4 in this case, since it hasn't been 30 days since August 6 and August 5 has already been done.
I am unsure what kind of function to use to do this counting. I appreciate your help
EDIT:
Date - 30day Value
July 1 - Yes
July 5 - No
August 1 - No
August 5 - No
August 6 - Yes
Today's Date is September 2.
The table would display July 5 and August 1, as their 30day values are No, and they are more than 30 days from todays date.

You should use DATEDIFF function:
SELECT ....
FROM your_table
WHERE DATEDIFF(CURDATE(), event_date) = 30
Where event_date is example of your date column.

MySQL's DATEDIFF function allows you to subtract 2 dates in a query.
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_datediff
DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.
For example:
SELECT some_id, date_column
FROM date_table
WHERE DATEDIFF(CURDATE(), date_column) = 30
You could also select both 30 and 60 days like this and also have a cutoff date of 60 days so it's not searching the whole table:
SELECT some_id, date_column
FROM date_table
WHERE date_column>=DATE_SUB(CURDATE(), INTERVAL 60 DAY)
AND DATEDIFF(CURDATE(), date_column) IN (30, 60)
And since I'm making some assumptions with my understanding of what you're asking, you may also want to do this which will return the results as 'Yes' or 'No' in your result set:
SELECT some_id, date_column,
CASE DATEDIFF(CURDATE(), date_column)
WHEN 60 THEN 'Yes'
WHEN 30 THEN 'Yes'
ELSE 'No'
END CASE AS is_3060_day
FROM date_table
WHERE date_column>=DATE_SUB(CURDATE(), INTERVAL 60 DAY)
Alternatively if you want to accomplish this on the PHP side, you could use PHP's date_diff function:
http://php.net/manual/en/function.date-diff.php
function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
{
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);
$interval = date_diff($datetime1, $datetime2);
return $interval->format($differenceFormat);
}
$result = dateDifference($date1, $date2)
if ($result==30 || $result==60) {
// Do something
}

you can fetch both the dates and use the php function
$prevdate = date_create("2013-03-15");
$currdate = date_create("2013-12-12");
$diff = date_diff($prevdate,$currdate);
echo $diff->format("%R%a days");
Output
272 days

Related

How to get last 3 days records from database without today?

I am using this code:
WHERE date > DATE_SUB(CURDATE(), INTERVAL 3 DAY)
And result will be like this:
2022-05-12
2022-05-11
2022-05-10
But I want this:
2022-05-11
2022-05-10
2022-05-09
Use a range here:
WHERE date < CURDATE() AND date >= DATE_SUB(CURDATE(), INTERVAL 3 DAY)
Assuming today's date be 2022-05-12, the above logic would exclude this date but include the three previous days, from 11th May to 9th May.

SUM multiple date ranges in mysql query

I have a MySql table 'Products' with three columns:
Date | Product | Qty
My aim is to SUM the qty of each product for every week.
Getting the SUM between two given dates would be easy:
SELECT SUM(qty) FROM products WHERE date >= '2010-01-01' AND date < '2011-01-1'
I can generate a list of weeks in Php using something like:
$today = date('Y-m-d', strtotime('last week Monday'));
$future = date('Y-m-d', strtotime('+90 days'));
$period = new DatePeriod(
new DateTime($today),
new DateInterval('P7D'),
new DateTime($future)
);
foreach ($period as $key => $value) {
$dates .= $value->format('Y-m-d');
}
Is there a MySql query that would work to Group By and SUM by dates? Or would I be better off looping through in Php?
You can use Year() and Week() functions in MySQL, to get the year and week number for a given date. Week() function will return week number from 0 to 53. So, you will be needed to use Year() function alongwith, if you have data spanning over multiple years.
But then, you will be more interested in knowing the start date and end date for the concerned week. This is where we can use a very interesting function DayOfWeek(). It returns the weekday index for a given date (1 = Sunday, 2 = Monday, …, 7 = Saturday)
We can use Date_Add() function using the weekday index value and the actual date value, to determine the starting week date and ending week date for a given date.
Try the following (if the Week starts on Sunday) :
SELECT
DATE_ADD(`date`, INTERVAL(1 - DAYOFWEEK(`date`)) DAY) AS week_start_date,
DATE_ADD(`date`, INTERVAL(7 - DAYOFWEEK(`date`)) DAY) AS week_end_date,
SUM(qty)
FROM
products
GROUP BY week_start_date, week_end_date
If the week starts on Monday, another handy function is WeekDay(). It returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday).
Try the following (if the Week starts on Monday) :
SELECT
DATE_ADD(`date`, INTERVAL(0 - WEEKDAY(`date`)) DAY) AS week_start_date,
DATE_ADD(`date`, INTERVAL(6 - WEEKDAY(`date`)) DAY) AS week_end_date,
SUM(qty)
FROM
products
GROUP BY week_start_date, week_end_date
You can achieve that with a group by like
GROUP BY week(date)
GROUP BY so do
SELECT SUM(qty) FROM products GROUP BY WEEK(date);

Get last 24 hours data from SQL

Hi all i want to select from my table all records from last 24h , 7days , 14 days ....( my sql timespan format is date("l, M d Y, h:i:s A")) so when i try to receive the data from last 24h is not working for me any help plz
exemple
if i get all date from
$result = mysql_query("SELECT * FROM stats");
result is Day,Date,time Count
Saturday, Feb 27 2016, 02:28:59 PM 27191 Saturday, Feb 27 2016, 03:28:05 PM 28659 Saturday, Feb 27 2016, 04:27:26 PM 30138
so i try like that and not working any help plz
$result = mysql_query("SELECT * FROM stats WHERE timespan >= now() - INTERVAL 1 DAY");
SELECT * FROM stats WHERE STR_TO_DATE(timespan,"%W, %b %d %Y, %h:%i:%s %p") >= date_sub(now(), INTERVAL 1 DAY)
STR_TO_DATE will convert your the string into date format (Y-m-d H:i:s) and then you can compare it with the date returned by now()
This should work. Make sure "timespan" exists and that it is a date. According to your example you should test on the "date" field.

get records of today, yesterday and this week [duplicate]

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'";

SQL query to show rows that are 60+ in the past

I have a row in my database with the datetime as
2013-11-07 13:04:57
then i am running this Query:
SELECT * from customer_communication
WHERE customer_seq = '276'
and datetime < DATE_SUB(DATE(now()), INTERVAL 60 DAY)
order by datetime DESC
so it should not be returning this row
i want to show all rows with a datetime that are 60+ days in the past
You can use DATEDIFF function
For example,
DATEDIFF(date1, date2) < 60
The date 2013-11-07 13:04:57 is more than 60 days in the past:
SELECT DATE_SUB(DATE(NOW()), INTERVAL 60 DAY);
=> 2013-11-10
Any date on or before November 10th will be displayed (from January 9th)
# Show difference in days.
SELECT DATEDIFF(DATE(NOW()),'2013-11-07');
=> 63
Additionally, you should be using the <= comparator if you wish to show rows that have a date set to exactly 60 days ago from the current day.
The problem is that your current query will select rows that are older than one datetime value. You need to change comparison sign to:
datetime > DATE_SUB(DATE(now()), INTERVAL 60 DAY)
And date 2013-11-07 13:04:57 more than 60 days in the past.

Categories