【问题标题】:To check weather the date selected by date picker is yesterday要检查天气,日期选择器选择的日期是昨天
【发布时间】:2015-05-22 07:24:11
【问题描述】:

用户输入入住和退房日期
我想查看天气,入住日期不是昨天,可以是今天。
&
天气退房日期至少比今天多一天。
(即从明天开始但不是今天)
日期格式:23-05-2015 (dd-mm-yyyy)

已尝试使用 (strtotime) 将日期转换为字符串,但它也会计算小时数,甚至在使用 strtotime 时将当前日期显示为上一个日期
:(
有没有办法做到这一点..?

谢谢

【问题讨论】:

    标签: php date jquery-ui-datepicker


    【解决方案1】:

    您可以构建 DateTime 对象并比较它们。

    <?php
    $checkin = new DateTime($_POST['checkin']);
    $checkout = new DateTime($_POST['checkout']);
    $today = new DateTime();
    
    if ($checkin >= $today && $checkout > $checkin) {
        // proceed to order
    }
    

    【讨论】:

    • 感谢您的回复。我可以看到您使用了 php 类 DateTime。
      我怎样才能在我的 php 站点中包含该类..?
    • DateTime 是一个原生 PHP (>= PHP 5.2.0) 类,你不需要任何东西来使用它:) 见:php.net/manual/fr/class.datetime.php。在使用日期时,你应该经常使用它,非常方便。例如,您可以进行简单的日期操作:$today = new DateTime(); $tomorrow = $today-&gt;modify('+1 day'); $lastDayOfMonth = $today-&gt;modify('last day of this month');
    【解决方案2】:

    我不确定我是否能完全理解你想要什么。但是,如果您只是想检查酒店预订日期间隔是否有效,那么这里有一个非常简单的代码 sn-p 可以帮助您:

    <?php
    $checkIn = '22-05-2015';
    $checkOut = '24-05-2015';
    
    $checkInTime = strtotime($checkIn);
    $checkOutTime = strtotime($checkOut);
    $todayTime = strtotime(date('Y-m-d'));
    
    if ($checkInTime < $todayTime) {
        echo 'Can not have a checkIn day in the past!';
    }
    if ($checkOutTime < ($checkInTime + 60 * 60 * 24)) {
        echo 'Have to stay at least one night!';
    }
    

    【讨论】:

    • 谢谢你,这很简单,可怜的我。
    猜你喜欢
    • 2011-08-05
    • 2013-03-15
    • 2017-06-24
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    相关资源
    最近更新 更多