Jump to content

Query Date Range


Canman2005

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

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.