Subtract date which is retrieve from mysql database by 2 days using PHP

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?

Answers


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

You have asked for a day which is 2 days before expiry day .

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

strtotime('+2 days', $expdate)

Second argument must be a timestamp

http://php.net/strtotime


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.


Need Your Help

Java Int Array returning all 0's

java arrays eclipse char int

Hello all and thanks for taking the time to look at my question. I'm working on my Java homework (I understand the rules and I don't want you to do my homework, I'm just very stuck and am very conf...

Chrome showing blank TypeScript file

google-chrome visual-studio-2013 typescript source-maps

I am setting up a new TypeScript project in visual studio 2013.