从事件中选择事件按日期字段选择?

时间:2011-06-30 10:23:47

标签: mysql database-design

我有一个名为 events 的表,有5个字段。在这些字段中,我有 eventDate 字段(日期类型字段),我在其中保留事件发生的日期。

我制作了一个下拉列表,其中包含从事件表格中进行选择的选项 1.所有事件(仅包含今天和未来的事件,不包括过去的事件)
2.本周活动
3.下周的活动
4.下个月的活动

在MYSQL中,smbd可以帮助我使用SELECT语句吗?

2 个答案:

答案 0 :(得分:1)

function x_week_range($date) {
                $ts = strtotime($date);
                $start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
                return array(date('Y-m-d', $start),
                                         date('Y-m-d', strtotime('next saturday', $start)));
        }


        //all events


        $sql = "SELECT * FROM events WHERE eventDate>='".date("Y-m-d")."' ";
        echo $sql."<br />";


        //current week 

        list($start_date, $end_date) = x_week_range(date("Y-m-d"));

        $sql = "SELECT * FROM events WHERE eventDate>='$start_date' AND eventDate<'$end_date'";
        echo $sql."<br />";


        //next week 
        $nextweek  = mktime(0, 0, 0, date("m")  , date("d")+7, date("Y"));
        $nextweek_day =  date("Y-m-d",$nextweek);


        list($start_date, $end_date) = x_week_range($nextweek_day);

        $sql = "SELECT * FROM events WHERE eventDate>='$start_date' AND eventDate<'$end_date'";
        echo $sql."<br />";

        //next month
        $m = date("m");
        $y = date("Y");

        $start_date = date("Y-m-d", mktime(0,0,0,$m+1,1,$y));
        $end_date = date("Y-m-d", mktime(0,0,0,$m+2,1-1,$y));

        $sql = "SELECT * FROM events WHERE eventDate>='$start_date' AND eventDate<'$end_date'";
        echo $sql."<br />";

答案 1 :(得分:0)

您可以使用php获取今天的日期和所有其他日期,并将其转换为sql格式 在php:

date_default_timezone_set('UTC');  
$today= mktime();
$dateSQL=date('Y-m-d',$today);  
$weekBegin=$today;  
while (date('w',=$weekBegin)!=0){  
$weekBegin=$weekBegin-24*60*60;  
}

1. SELECT * FROM events WHERE eventDate>='$today'
2. SELECT * FROM events WHERE eventDate>='$weekBegin' AND eventDate<'$weekBeginPlus7'

等等。