Jump to content

Increment Date


jcstanley

Recommended Posts

Hi

 

What I am trying to do is select items from a databse but only between two dates, but I am not sure how I can make the $week variable = today + 7 days  and the    $month variable = today + 1 month.

 

$today = date("Y-m-d");
$week = ?
$month =  ?

 

I guess the SQL would look something like

 

SELECT * FROM table BETWEEN $today AND $week

 

Any ideas?  :)

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/40727-increment-date/
Share on other sites

Thanks

 

I have managed to increment the dates and have checked that it does work, but now having trouble with the SQL

 

This is what I have but finds no rows.

 

$today = date('Y-m-d');
$month = date('Y-m-d', strtotime('+1 month'));
.
.
.
$query = "SELECT eventid, name, DATE_FORMAT(date, '%d %b %y') AS date, time, type, extra FROM events WHERE type = 'Race' AND date BETWEEN $today AND $month ORDER BY 'date' asc";

 

Not sure what has gone wrong, and yes there are events in the databse within the date range  ;)

 

 

Link to comment
https://forums.phpfreaks.com/topic/40727-increment-date/#findComment-197174
Share on other sites

I wouldn't use PHP at all for this since you're using it within a MySQL query. You're much better off letting MySQL do the work for you:

SELECT eventid, name, DATE_FORMAT(date, '%d %b %y') AS formatDate, time, type, extra
FROM events
WHERE type = 'Race' AND date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY);

 

Notice that to run your date comparison, you have to select your DATE_FORMAT result as a different name in order to return accurate results.

Link to comment
https://forums.phpfreaks.com/topic/40727-increment-date/#findComment-197183
Share on other sites

Thanks that works perfectly

 

Can the same be done with an interval of 1 Month?

 

Definitely. Also, to make it more readable, and as I'm sure fenway would be quick to point out, the following is cleaner:

BETWEEN CURDATE() AND CURDATE + INTERVAL 7 DAY;

 

Check out the MySQL Date and Time functions for more details about INTERVAL.

Link to comment
https://forums.phpfreaks.com/topic/40727-increment-date/#findComment-197193
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.