使用php查找给定的日期在需要的范围内


Finding given date is in the wanted range using php

我有一个任务来输入数据,并使用php和mysql生成报表。在给定的开始日期和结束日期范围内从数据库检索数据。在生成的报告中,如果记录的输入时间满足以下条件之一,我想让用户通过使用"contenteditable"来编辑数据。

1。如果记录录入时间在本周

2。如果当前时间在星期二午夜之前,用户可以编辑上周记录,该记录的输入时间为上周

记录的录入时间格式如下

2015-07-31 18:44:30

然后

if (record entry time satisafy abovw 2 points){
data with that record entry time can edit
}
else{
canit edit
}

我怎样才能找到时间范围做以上。请帮帮我。现在我正在做这个

 //getting today's date
            $timestamp = strtotime(date("Y-m-d"));//getting today's date
            $day = date('w', $timestamp);
         //   print_r($day);
            //according to today's date defining content editable records
            if($day==0){
                $start_edit_date=date('Y-m-d',strtotime('last monday'));
                $end_edit_date= date("Y-m-d") ;
            }
            if($day==1){
                $start_edit_date=date('Y-m-d',strtotime('last monday'));
                $end_edit_date= date("Y-m-d")  ;
            }
            if($day==2){
                $start_edit_date=date('Y-m-d', strtotime('-8 days'));
                $end_edit_date=date("Y-m-d") ;
            }
            if($day==3){
                $start_edit_date=date('Y-m-d',strtotime('last monday'));
                $end_edit_date=date("Y-m-d") ;
            }
            if($day==4){
                $start_edit_date=date('Y-m-d',strtotime('last monday'));
                $end_edit_date=date("Y-m-d") ;
            }
            if($day==5){
                $start_edit_date=date('Y-m-d',strtotime('last monday'));
                $end_edit_date=date("Y-m-d")  ;
            }
            if($day==6){
                $start_edit_date=date('Y-m-d',strtotime('last monday'));
                $end_edit_date=date("Y-m-d")  ;
            }
            $start_edit_date=$start_edit_date." 00:00:00";
            $end_edit_date=$end_edit_date." ".date("H:i:sa");
            echo"You can edit your record between ". $start_edit_date." and ".$end_edit_date;

它工作得很好,但是有没有更好的方法来获取范围

你可以用一个函数来缩短这个脚本:

<?php
function GetDates($timestamp = false)
    {
        $today      =   date("Y-m-d");
        $timestamp  =   ($timestamp != false)? $timestamp : strtotime($today);
        $day        =   date('w',$timestamp);
        $use[0] = $use[1] = $use[3] = $use[4] = $use[5] = $use[6] =  date('Y-m-d',strtotime('last monday'));    
        $use[2] = date('Y-m-d', strtotime('-8 days'));
        $use_dates['start'] =   $use[$day]." 00:00:00";
        $use_dates['end']   =   $today." ".date("H:i:sa");
        return $use_dates;
    }
// Assign dates
$dates  =   GetDates(); ?>
<p>You can edit your record between <?php echo $dates['start']; ?> and <?php echo $dates['end']; ?>.</p>