简体   繁体   中英

Date Ranges between 2 dates (Syntac)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM