I want to take date which should be before 2 days of expiry date. I am getting Expiry date from MYSQL database. Here is my code:
$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;
}
But I am getting output like this:
Start date:2012-05-01
Expiry date:2012-06-30
2 Days before Expiry date:1970-01-03
Can you help me please?
http://php.net/manual/en/function.strtotime.php
The 2nd argument of PHP's strtotime() function expects a Unix timestamp. Try this instead:
$date=date('Y-m-d',strtotime($expdate.' +2 days'));
The strtotime function takes two parameters
int strtotime ( string $time [, int $now = time() ] )
The second should be an integer, but it looks like you are passing it a string. You need to convert it to an integer first. This should work:
$expdate_int = strtotime($expdate);
$date = date('Y-m-d', strtotime('+2 days', $expdate_int));
If appropriate, you could also look at doing the date maths in SQL
SELECT expdate, DATE_SUB(expdate, INTERVAL 2 DAY) AS two_days_before_exp
您要求的是到期日前2天的一天。
$date=date('Y-m-d',strtotime($expdate.'-2 days'));
Change your following line
$date=date('Y-m-d',strtotime('+2 days', $expdate));
to following
$date=date('Y-m-d',strtotime('-2 days', strtotime($expdate)));
strtotime second parameter expects timestamp (not a string date). "-2" to make it 2 days before.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.