Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.