-1

我想取应该在到期日 2 天之前的日期。我从 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 

你能帮我吗?

4

5 回答 5

3

http://php.net/manual/en/function.strtotime.php

PHP 的 strtotime() 函数的第二个参数需要一个 Unix 时间戳。试试这个:

$date=date('Y-m-d',strtotime($expdate.' +2 days'));
于 2012-06-13T11:04:46.623 回答
2

strtotime 函数有两个参数

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
于 2012-06-13T11:11:37.093 回答
0

您要求的日期是到期日前 2 天。

  $date=date('Y-m-d',strtotime($expdate.'-2 days'));
于 2012-06-13T11:07:17.510 回答
0
strtotime('+2 days', $expdate)

第二个参数必须是时间戳

http://php.net/strtotime

于 2012-06-13T11:07:46.907 回答
0

更改以下行

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

跟随

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

strtotime 第二个参数需要时间戳(不是字符串日期)。“-2”表示提前 2 天。

于 2012-06-13T11:27:47.087 回答