简体   繁体   English

如何使用if语句显示属于日期范围的记录

[英]How to show records that falls on the date-range using if statement

I want to display all the records during August of the current year up to December of the current year. 我想显示当年8月到当年12月的所有记录。 While it will not display on January up to June, Instead the Records of January up to June will be displayed. 虽然它将不会显示到6月的1月,但是会显示1月到6月的记录。 How will I catch the date using IF statement? 如何使用IF语句捕获日期?

Here is my code: 这是我的代码:

            $year = date("Y");
            $sem1_start = date("M-d-Y", strtotime("August, 01".$year));
            $sem1_end = date("M-d-Y", strtotime("December, 31".$year));

            $sem2_start = date("M-d-Y", strtotime("January, 01".$year));
            $sem2_end = date("M-d-Y", strtotime("June, 15".$year));

            //sample date from database
            $date = '2015-10-15';
            $date2 = date("M-d-Y", strtotime($date));


            if($date2 >= $sem1_s && $date2 <= $sem1_e){
                //Display all records
            }
            else if($date2 >= $sem2_s && $date2 <= $sem2_e){
                 //Display all records
            }

I wanted to automatically display the records of the current semester (August-December) and when January comes, it will hide the records but will display the records of the second semester which is January up to June. 我想自动显示当前学期(八月至十二月)的记录,当一月到来时,它将隐藏记录,但将显示第二学期(即一月到六月)的记录。

You could filter your sql result with the between statement. 您可以使用between语句过滤sql结果。 Something like 就像是

$query = "select * from db_table where datefield BETWEEN $startdate and $enddate order by datefield asc; $ query =“从db_table中选择*,其中datefield在$ startdate和$ enddate之间按da​​tefield asc排序;

That way your results will always fall in range and are sorted to boot 这样一来,您的结果将始终落在范围内,并进行排序以进行引导

I think, in the database I have a field to save valuable time 我认为,在数据库中,我有一个领域可以节省宝贵的时间

$time = time();

When I want to search from August of the current year to March 12 of the current year I will do the following 当我要搜索当年8月至当年3月12日时,我将执行以下操作

$timeSearchStart = strtotime(date('Y') . '-8-1 00:00:00');
$timeSearchEnd = strtotime(date('Y') . '-12-31 23:59:59');
$query = "SELECT * FROM `table` WHERE `fieldSaveTime` > '" . $timeSearchStart . "' && `fieldSaveTime` < '" . $timeSearchEnd  . "'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM