Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将从mysql数据库检索的日期减去2天_Php_Mysql - Fatal编程技术网

使用PHP将从mysql数据库检索的日期减去2天

使用PHP将从mysql数据库检索的日期减去2天,php,mysql,Php,Mysql,我想要的日期应该是到期日前两天。我从MYSQL数据库中获取到期日期。这是我的密码: $result=mysql_query("SELECT * from assets"); while($row=mysql_fetch_array($result)) { echo "Start date:".$row["start_date"]; echo "Expiry date:".$row["expiry_date"]; $expdate=$row["expiry_date"]; $date=d

我想要的日期应该是到期日前两天。我从MYSQL数据库中获取到期日期。这是我的密码:

$result=mysql_query("SELECT * from assets");

while($row=mysql_fetch_array($result))
{

echo "Start date:".$row["start_date"];

echo "Expiry date:".$row["expiry_date"];

$expdate=$row["expiry_date"];

$date=date('Y-m-d',strtotime('+2 days', $expdate));

echo "2 Days before Expiry date:".$date;

}
但我得到的输出是这样的:

Start date:2012-05-01

Expiry date:2012-06-30

2 Days before Expiry date:1970-01-03 
你能帮我吗?

PHP的strotime()函数的第二个参数需要一个Unix时间戳。请尝试以下方法:

$date=date('Y-m-d',strtotime($expdate.' +2 days'));

您要求的日期为到期日前2天

  $date=date('Y-m-d',strtotime($expdate.'-2 days'));
第二个参数必须是时间戳


strotime函数有两个参数

int strtotime ( string $time [, int $now = time() ] )
第二个应该是整数,但看起来像是在传递字符串。 您需要先将其转换为整数。这应该起作用:

$expdate_int = strtotime($expdate);
$date = date('Y-m-d', strtotime('+2 days', $expdate_int));
如果合适,您还可以考虑使用SQL进行日期计算

SELECT expdate, DATE_SUB(expdate, INTERVAL 2 DAY) AS two_days_before_exp

改变你的下一行

$date=date('Y-m-d',strtotime('+2 days', $expdate));
追随

$date=date('Y-m-d',strtotime('-2 days', strtotime($expdate)));

strotime第二个参数需要时间戳(不是字符串日期)。“-2”使其提前2天运行。

请不要使用
mysql.*
函数编写新代码。它们不再得到维护,社区已经开始。看到了吗?相反,你应该学习并使用或。如果你不能决定哪一个,我会帮你的。如果您选择PDO。
$date=date('Y-m-d',strtotime('-2 days', strtotime($expdate)));