PHP检查今天是否在两个日期之间 [英] PHP check if today is in between two dates

查看:54
本文介绍了PHP检查今天是否在两个日期之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

$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';

即使$ today在这些开始日期和结束日期之间,我也会得到否"的回报.这可能是什么问题?我只想检查今天是否在这些日期之间.开始日期和结束日期以字符串形式保存在数据库中.

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.

推荐答案

尝试一下:

<?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";
    }

?>

这篇关于PHP检查今天是否在两个日期之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆