如何设置格式正确的数值

时间:2019-03-17 20:50:14

标签: php

我试图找出两个日期之间的差异(一个是用户注册日期,另一个是当前日期)。如果当前日期距离注册日期还有一天,那么该消息将弹出“您有能力献血”。但是它显示错误“遇到格式错误的数值”。因为我是初学者,这对我来说是个大问题。期待您的帮助。预先感谢。

这是我的代码

 <?php 

                   $date= Session::get("donation_date");
                   if (isset($date)) {
                        $today= time();
                        $difference=($date- $date) > 1;
                        echo ' '.$difference.' you are capable to donate blood';
                   }


              ?>

2 个答案:

答案 0 :(得分:1)

您一方面使用Unix时间,另一方面使用字符串。
Unix时间是一个发送计数器,其值为整数。
日期是(我假设)像2019-03-17这样的字符串。

您可以使用strtotime将日期解析为Unix,这样您就可以减去两个整数。
结果是以秒为单位的差异,表示86400是一天(不考虑DST)。

$date= Session::get("donation_date");
if (isset($date)) {
    $today = time();
    $difference = $today - strtotime($date);
    if ($difference > 864000) echo ' '.$difference.' you are capable to donate blood';
}

编辑:为了使strtotime能够解析日期,则日期必须是可解析的格式。
example
如果不是,那么就这样说,我将帮助您解析日期。

答案 1 :(得分:0)

好像您有错字:

$difference=($date-$date) > 1;

应为:

$difference=($today-$date) > 1;

此外,最好确保$date的格式正确,请更新:

if (isset($date)) {...}

收件人:

if (is_numeric($date)) {...}
始终将{p> as $date设置为直接在上面分配的值。 希望有帮助,