Date Ranges between 2 dates (Syntac) - php

This is driving me crazy!!!
Here is the code that I am trying to put data for the last 31 days between 2 dates, NOW and 31 days later.
This code does not work:
$now = date("Y-m-d");
$datetime = new DateTime($now);
$datetime->modify('+31 days');
$NEW_30 = $datetime->format('Y-m-d');
$get_time1ax = "select * from support_tickets WHERE start_date >= '".$now."' AND end_date <= '".$NEW_30."' ORDER BY problem_title ASC";
But this code does work? Do not understand this.
$get_time1ax = "select * from support_tickets WHERE start_date >= '2020-03-19' AND end_date <= '2020-04-19' ORDER BY problem_title ASC";
Notice the actual date is in the field and not a variable. Weird.
Any help would be helpful.

I am trying to put data for the last 31 days between [...] now and 31 days later.
You can do date arithmetics directly in the database:
select *
from support_tickets
where start_date >= curent_date and end_date <= current_date + interval 31 day
Possibly, you mean 1 month when you say 31 days, so:
select *
from support_tickets
where start_date >= curent_date and end_date <= current_date + interval 1 month

try this:
$current_date = date("Y-m-d");
$current_date_plus_31 = date('Y-m-d',strtotime('+31 days',strtotime($current_date)));
this is how it works for me with today's date and in 31 days

Related

PHP/MYSQL get tomorrow records from

I have a varchar field contain a timestamp value, i want to get the tomorrow records.
This is my code as you can see:
$tomorrow = strtotime("1+ day");
SELECT * FROM tasks WHERE task_accomplish_date > $tomorrow
but the query is incorrect
thanks for the help in advance
Should be like this :
$tomorrow = strtotime("+1 day");
/* this will select all record before tomorrow*/
SELECT * FROM tasks WHERE task_accomplish_date < $tomorrow;
/* this will select all record after tomorrow*/
SELECT * FROM tasks WHERE task_accomplish_date > $tomorrow;
http://php.net/manual/en/function.strtotime.php
SELECT * FROM tasks WHERE task_accomplish_date > NOW() + INTERVAL 1 DAY
You should be able to do this all in the MySQL query
SELECT *
FROM `tasks`
WHERE DATE(`task_accomplish_date`) = DATE(NOW() + INTERVAL 1 DAY)
That converts your task_accomplish_date into a date value and looks for records where that date is equal to today + 1 day (tomorrow). That does not give you records from 2 days from today or beyond, just tomorrow. If you want tomorrow and all records beyond tomorrow you would use
SELECT *
FROM `tasks`
WHERE DATE(`task_accomplish_date`) >= DATE(NOW() + INTERVAL 1 DAY)
If you are storing a timestamp, these only care about the date part of the timestamp, not about the time part of the timestamp. If you care about the time part as well you can remove DATE() from both sides of the = in the WHERE clause

Puling out data from the table from last week

$qry = "SELECT * FROM school WHERE WEEKOFYEAR(date) = WEEKOFYEAR(NOW()) ";
This query that i have currently in my code is getting me data from the current week starting from Monday. How can i get data from the table from last week or the week before. I have tried changing now to last week or currentweek.
any ideas is it possible to use weekofyear(()) and tweak it
You can do some date shift right in mysql, replace NOW() with
NOW() - INTERVAL 1 WEEK
there is also date_sub for subtracting
date_sub(NOW(),INTERVAL 1 WEEK)
Try this.
$qry = "SELECT * FROM school WHERE studId = $sessionId AND studentId = $student
AND date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY"

How to pull week's worth of appointments with CURDATE + 7 Day INTERVAL

I have prepared a little pull request to pull appointments for Today from the DB:
$getAppointmentsToday = $db->prepare("SELECT * FROM appointments WHERE DATE(appointment_date) = CURDATE()");
$getAppointmentsToday->execute();
I tried modifying this statement to also pull appointments for the following 7 days, but running into some trouble:
$getAppointmentsWeek = $db->prepare("SELECT * FROM appointments WHERE DATE(appointment_date) = (CURDATE(), INTERVAL 7 DAYS)");
$getAppointmentsWeek->execute();
You can use the BETWEEN operator for this :
...
WHERE DATE(appointment_date)
BETWEEN CURDATE()
AND CURDATE() + INTERVAL 7 DAY
You are now checking the date CURDATE()+7 days, instead of the date interval. Use:
SELECT * FROM appointments WHERE DATE(appointment_date) <= (CURDATE(), INTERVAL 7 DAYS) AND DATE(appointment_date) >= CURDATE()

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

get data from last week

I want all data from last week.
I used
SELECT id FROM tbl
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
but its not working when my week starts with "Monday".
What should I do?
SELECT id FROM tbl WHERE date >= CURDATE() - INTERVAL (WEEKDAY(CURDATE())+7) DAY AND date < CURDATE() - INTERVAL (WEEKDAY(CURDATE())) DAY
I try this and it work for me.
Try
SELECT id FROM tbl
WHERE YEARweek(date) = YEARweek(curdate())
Then change like this
$lastweek = unix_to_human(time("Y-m-d H:i:s") - (7 * 24 * 60 * 60), TRUE, 'us');
SELECT id FROM tbl
WHERE date >= curdate() - $lastweek
AND date < curdate() - $lastweek

Categories