检查两个日期之间的日期-PHP



我想检查DB中存储的某个日期,如果此日期是在此财政年度中,它将打印有效,如果不是,则打印strong>无效

这是我的php代码:

$init_date= date("2016/07/01");
$end_date= date("2017/06/30");
$i_date = strtotime($init_date);
$e_date = strtotime($end_date);
$date_db= strtotime($date);// this $date is retrieved from my DB
if ($e_date > $date_db && $i_date < $date_db) 
     { echo "valid";}
else { echo "invalid";}

,但问题是我不想设置起点,而结束日期则是有机化的方法吗?因为它将每年更新

这样,您的脚本将有点 dynamic

// the below snippet checks the date retrieved from database
// against fiscal periods between years 2000 and 2050 and return the 
// valid dates
$endYear=2000;
while($endYear<=2050) {
$end = $endYear.'/06/30';
$endDate = DateTime::createFromFormat('Y/m/d', $end);
$initDate = DateTime::createFromFormat('Y/m/d', $end);
$initDate = $initDate->sub(new DateInterval('P1Y'))->add(new DateInterval('P1D'));
$ddb = '2016-09-27';
$dateFrodDB = DateTime::createFromFormat('Y-m-d', $ddb);
if ($dateFrodDB>=$initDate && $dateFrodDB<=$endDate) 
     { echo "validn";
         echo "tStartDate->"".$initDate->format("Y-m-d").""n";
         echo "tEndDate->"".$endDate->format("Y-m-d").""n";
         echo "tDateFromDatabase->"".$dateFrodDB->format("Y-m-d").""n";
     }
$endYear++;
}
/* output
   valid
        StartDate->"2016-07-01"
        EndDate->"2017-06-30"
        DateFromDatabase->"2016-09-27"
*/

在PHP沙盒上检查此信息

尝试这个,

<?php
$start_date = date('Y-m-d', strtotime(str_replace("/", "-", $initdate)));
$end_date   = date('Y-m-d', strtotime(str_replace("/", "-", $end_date)));
$new_date   = date('Y-m-d', strtotime(str_replace("/", "-", $date)));
 if (($start_date < $new_date && $new_date < $end_date) || ($end_date < $new_date && $new_date < $start_date)) {
    echo "valid";
} else {
    echo 'invalid';
}

这是工作链接

相关内容

  • 没有找到相关文章

最新更新