MYSQL / PHP如何从日期起减去7天?

时间:2022-04-02 19:10:01

i am using a system that tells me how many days out of 7 days it has taken for a user to re-submit data in a form to the database. the below script measures how many days have past from the original start date / when the form was originally filled out to when it was completed.

我正在使用一个系统,告诉我用户将表单中的数据重新提交到数据库的7天中有多少天。下面的脚本测量从原始开始日期/表单最初填写到完成时间的过去天数。

basically i am trying to echo 2 different measures, if the days that have past from the start date are less than 7, then it should echo out '(number of days) out of 7 past'

基本上我试图回应两种不同的措施,如果从开始日期开始的日期小于7,那么它应该回显“过去7天的'(天数)”

i.e. 1 day of 7 past or 4 days of 7 past

即7个过去的1天或7个过去的4天

and if it has gone over 7days, it should then say '(number of days) overdue'. the problem i am getting is that i am allowing my users 7 days to submit the form. bearing this in mind they are not overdue therefore untill it has gone past the 7 days timeframe, therefore if a user takes 8 days to complete the form they should only be one day overdue, however my script currently says 8 days overdue, as it takes into consideration the earlier 7.

如果已超过7天,则应说“(天数)过期”。我得到的问题是,我允许我的用户7天提交表格。考虑到这一点,他们并没有过期,因此它已经超过7天的时间范围,因此如果用户需要8天才能完成表格,他们应该只有一天过期,但是我的脚本目前说8天过期,因为它需要考虑到早先的7。

Is there a way i can minus 7 days to show once a user goes over the 7 day time frame?

有一种方法,一旦用户超过7天的时间框架我可以减去7天吗?

Thanks

<?php include 'config.php';
     $data = mysql_query("SELECT *, TIMESTAMPDIFF(DAY, date, CURDATE()) AS expire_date FROM supplier_session ORDER BY expire_date ASC") 
     or die(mysql_error()); 

     echo "<table class=\"table\" style=\"width:995px;  font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
     font-size:11px;\" >

<tr>
    <td style=\"width:100px;\">ID:</td><td>Company Name:</td><td>Company Reg No:</td><td>Application Started:</td><td style=\"width:200px;\">Application Duration:</td><td style=\"width:100px;\">Date:</td><td>Status:</td></tr>";


     while($row = mysql_fetch_array( $data )) { 
        $days = $row['expire_date'];
        $when = $days*0; 
        $str = $row['expire_date'];
        $str2 = substr($str, 0); // "quick brown fox jumps over the lazy dog."
       if ($when <= 31){
       echo "<tr><td style=\"width:100px;\"><p>".$row['id'] . "</p></td>"; 
       echo "<td style=\"width:150px;\"><p>".$row['company_name'] . "</p></td>"; 
       echo "<td style=\"width:150px;\"><p>".$row['company_reg_number'] . "</p></td>";
       echo "<td>"; echo date('d/m/Y',strtotime($row['date'])); echo "</td>";


       if ($days >= 8) {
            echo "<td style=\"width:200px;\"><p>{$str2} days overdue</td>";
       }

        elseif ($when <= 7){


             echo "<td style=\"width:200px;\"><p>{$str2} of 7 days past</td>";
        }




        }


        echo "<tr>";
      }


      echo "</table>"; //Close the table in HTML


    ?>

2 个解决方案

#1


33  

If you want 7 days back from today you can use this:

如果你想从今天开始7天后你可以使用这个:

date('Y-m-d', strtotime('-7 days'))

#2


1  

$date = $row['expire_date']; //date from database 
$str2 = date('Y-m-d', strtotime('-7 days', strtotime($date))); 

#1


33  

If you want 7 days back from today you can use this:

如果你想从今天开始7天后你可以使用这个:

date('Y-m-d', strtotime('-7 days'))

#2


1  

$date = $row['expire_date']; //date from database 
$str2 = date('Y-m-d', strtotime('-7 days', strtotime($date)));