PHP check if today is in between two dates

三世轮回 提交于 2020-12-26 12:04:37

问题


Consider following code:

$today = date("d/m/Y");

$start_date = '20/11/2014';
$time1 = strtotime($start_date1);
$start_date1 = date('d/m/Y',$time1);

$end_date = '20/01/2015';
$time2 = strtotime($end_date1);
$end_date1 = date('d/m/Y',$time2);

if( $start_date1 <= $today && $end_date1 >= $today)
    echo "yes";
else
    echo 'no';

Even though $today is in between those start and end date, I get "no" in return. What might be the problem here? I just wanna check if today is in between those dates. Start date and end date are saved as string in DB.


回答1:


Try this:

<?php

    $now = new DateTime();
    $startdate = new DateTime("2014-11-20");
    $enddate = new DateTime("2015-01-20");

    if($startdate <= $now && $now <= $enddate) {
        echo "Yes";
    }else{
        echo "No";
    }

?>


来源:https://stackoverflow.com/questions/27863791/php-check-if-today-is-in-between-two-dates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!