php 检查今天的日期是否超过了到期日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37301753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:09:23  来源:igfitidea点击:

Check if today's date is passed the expiration date

php

提问by Henders

Can someone out there help me with this:

有人可以帮我解决这个问题吗:

<?php 
    include 'include/db_config.php';

    $result = $dbs->prepare("SELECT * FROM service_info WHERE id = :id");
    $result->bindParam(':id', $_SESSION['id']);
    $result->execute();
    for ($i=0;$i = 0; $row = $result->fetch(); $i++) {
        ?>

        <td><?php echo $row['datepicker']; ?></td>

        <?php



        $offset = strtotime("+1 day");
        $enddate = date("Y-m-d H:i:s", $offset);
        echo $enddate . "</br>";
        if ($enddate < $startdate) {
            echo "expired";
        } else {
            echo "active";
        }
    }
?>

What I want to achieve is to find out if the date value held in $row['datepicker']was more than 2 days ago. If it was more than 2 days ago I want it to echo expiredand otherwise I want it to show active.

我想要实现的是找出保存的日期值$row['datepicker']是否超过 2 天前。如果超过 2 天前,我希望它回显expired,否则我希望它显示active.

For example: $row['datepicker']could contain the date: May 18, 2016 (based on the users input - not a fixed value). That would mean it's expiration date would be: May 20,2016. If today's date is greater than or equal to May 20th 2016, it should echo expired. If today's date was May 19th 2016 it should echo activebecause it is not yet two days in the past.

例如: $row['datepicker']可能包含日期:2016 年 5 月 18 日(基于用户输入 - 不是固定值)。这意味着它的到期日期是:2016 年 5 月 20 日。如果今天的日期大于或等于 2016 年 5 月 20 日,则应为 echo expired。如果今天的日期是 2016 年 5 月 19 日,它应该会回显,active因为现在还不到两天。

回答by Henders

Based on your comments, you want to take the $startdateand add two days to it. If today's date is passed that time then you want to print expired and otherwise print active.

根据您的评论,您想花$startdate两天时间添加它。如果今天的日期在那个时间过去了,那么您要打印过期,否则打印活动。

$startdate = "16-May-2016";
$expire = strtotime($startdate. ' + 2 days');
$today = strtotime("today midnight");

if($today >= $expire){
    echo "expired";
} else {
    echo "active";
}

In this case it will echo expiredbecause, at the time of writing this, we are two days passed the 16th May. If you changed the $startdateto be 17th May it would echo activebecause that is only 1 day in the past.

在这种情况下,它会回响,expired因为在撰写本文时,我们距离 5 月 16 日已经两天了。如果您将日期更改为$startdate5 月 17 日,它会回响,active因为那只是过去的 1 天。

For your specific problem you would want to change $startdateto look like this:

对于您的特定问题,您希望更改$startdate为如下所示:

$startdate = $row['datepicker'];

Whenever the date specified in $startdateis greater than or equal to 2 days in the past it will show expiredotherwise it will show active.

每当 中指定的日期$startdate大于或等于过去 2 天时,它将显示,expired否则将显示活动。



What are we doing here?

我们在这里做什么?

It's quite simple. We take advantage of strtotime()to get the timestamp of the day it will be +2 daysfrom your $startdatevalue. Then we get today's date at midnight (so the actual timestamp would be the equivalent of 18-May-2016 00:00:00).

这很简单。我们利用strtotime()+2 days从您的$startdate价值中获取当天的时间戳。然后我们在午夜获得今天的日期(因此实际时间戳将相当于 18-May-2016 00:00:00)。

Our last step is to compare the two timestamps and see if today's date is greater than or equal to the expiry date.

我们的最后一步是比较两个时间戳,看看今天的日期是否大于或等于到期日。



Note:This assumes that your $startdateis in a format that is compatible with strtotime().

注意:这假设您$startdate的格式与strtotime().

回答by Amit Ray

In php 5.2+ you can use this

在 php 5.2+ 中你可以使用这个

$today = date("Y-m-d H:i:s");  
$startdate = $row['datepicker'];   
$offset = strtotime("+1 day");
$enddate = date($startdate, $offset);    
$today_date = new DateTime($today);
$expiry_date = new DateTime($enddate);

if ($expiry_date < $today_date) { // your echo stuff }