MYSQL/PHP 如何从日期减去 7 天?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23493707/
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
MYSQL/PHP how to minus 7 days from date?
提问by user3584968
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'
基本上我试图回应 2 种不同的措施,如果从开始日期过去的天数小于 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
?>
回答by dev4092
If you want 7 days back from today you can use this:
如果你想从今天起 7 天后退,你可以使用这个:
date('Y-m-d', strtotime('-7 days'))
回答by Dishant Bhatt
$date = $row['expire_date']; //date from database
$str2 = date('Y-m-d', strtotime('-7 days', strtotime($date)));
回答by Dharman
You can use strtotime()
but a better option is to use DateTime
class. It's easier to use and offers more.
您可以使用,strtotime()
但更好的选择是使用DateTime
类。它更易于使用并提供更多功能。
A compact way of subtracting 7 days:
减去 7 天的紧凑方法:
$date = '2020-02-03';
echo date_create($date)
->modify('-7 days')
->format('Y-m-d');
This is compacted into chained methods, but you can create an instance of the class on its own and perform actions on the object.
这被压缩为链式方法,但您可以自行创建类的实例并对对象执行操作。
$dateObject = new \DateTime($date);
$dateObject->modify ('-7 days');
The same can be done in SQL, so if you have no reason to do it in PHP you should do it in MySQL.
在 SQL 中也可以这样做,因此如果您没有理由在 PHP 中执行此操作,则应该在 MySQL 中执行此操作。
回答by Dlk
Given answers are just calculating for the year, month and day which is not calculating for hours, minutes, etc..
给出的答案只是计算年、月和日,而不是计算小时、分钟等。
So, if you updated your database last night at 23:59 then it will show 1 day past after 2 minutes at 00:01.
因此,如果您昨晚在 23:59 更新了数据库,那么它将在 2 分钟后的 00:01 显示过去 1 天。
Correct answer of this question should be formated like this
这个问题的正确答案应该是这样的
date('Y-m-d H:i:s');
will give us year, month, day, hour, minute and second of the current date Now
or expire_date.
date('Y-m-d H:i:s');
将为我们提供当前日期Now
或 expire_date 的年、月、日、小时、分钟和秒。
$expire_date = '2020-01-23 10:12:13'; //Output of the date we mentioned above
$a = date('Y-m-d H:i:s', strtotime('-7 days', strtotime($expire_date)));
echo $a; //Correct result of 7 days past, 2020-01-16 10:12:13
回答by Sayeed roshan
Tested , and its working fine You can Subtract date in php as below.
已测试,并且工作正常您可以在 php 中减去日期,如下所示。
$test_date = "2020-02-02";
date("Y-m-d", strtotime("-2 days", strtotime($test_date)));