Jump to content

Recommended Posts

Hi all

 

I have a table which holds a bunch of dates

 

id  datefrom      dateto

1    2007-02-22  2007-03-05

2    2007-05-13  2007-06-23

3    2007-05-25  2007-07-23

 

the `datefrom` and `dateto` are dates in the format YYYY-MM-DD, the `datefrom` means the date the event starts and the `dateto` means the date the event ends.

 

What I want to run a query on this table and return any rows which have todays date between the 'datefrom' and the 'dateto'.

 

Does that make much sense?

 

Can anyone help?

 

Thanks in advance

 

Dave

Link to comment
https://forums.phpfreaks.com/topic/54038-query-date-range/
Share on other sites

If your times were unix timestamps you would be doing this:

 

$today = time();

mysql_query("SELECT * FROM table WHERE datefrom < " . $today . " AND " . $today . "< dateto");

 

Now your only problem is that you haven't got timestamps but some other dateformat. You could play around with PHP's strtotime() function:

 

http://no.php.net/strtotime

 

but I don't know if there is a smarter way.

 

 

Link to comment
https://forums.phpfreaks.com/topic/54038-query-date-range/#findComment-267172
Share on other sites


# Select all events that occur between a range or start on a monday
SELECT * 
FROM table 
WHERE (
NOW() BETWEEN datefrom AND dateto 
OR DAYOFWEEK(datefrom) = 2
)

# Select all events that occur between a range AND start on a monday
SELECT * 
FROM table 
WHERE (
NOW() BETWEEN datefrom AND dateto 
AND DAYOFWEEK(datefrom) = 2
)

 

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_dayofweek

Link to comment
https://forums.phpfreaks.com/topic/54038-query-date-range/#findComment-267428
Share on other sites

<?php
$mon =  date('Y-m-d', strtotime('this monday'));
$fri =  date('Y-m-d', strtotime('this friday'));

$sql = "SELECT * FROM table 
        WHERE (datefrom BETWEEN '$mon' AND '$fri')
        OR (CURDATE() BETWEEN datefrom AND dateto) 
        ";
?>

Link to comment
https://forums.phpfreaks.com/topic/54038-query-date-range/#findComment-267444
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.