-2

I have searched this topic and came up with some answers but they don't seem to work. I am a beginner at PHP and I need to create a variable that tells if a clients original order date is older than 52 days from the current date. Here is what I have that is not working

$orderdate = $row['OrderDate'];

if(strtotime('$orderdate')<= strtotime('-52 days')){
 $pastdue ='This Order is older than 52 Days';
 }else{
 $pastdue ='This order is newer than 52 Days';
 }

The order date comes from a MySQL database and is formatted like '2014-12-11 08:45:56'. The problem is that every time I echo the $pastdue variable later in the script it uses the first variable 'This Order is older than 52 days'. It will not recognize if the order is between 0 and 52 days. Any ideas of what I am doing wrong? Or a better way of doing it? The field in the MySQL database is a datetime field.

  • You should use the SQL query to get the right rows (meaning apply a date filter to your query) directly from the database. Its inefficient to filter the results on PHP side. – Zeeshan Abbas Dec 11 '14 at 22:08

1 Answers1

3

Basic PHP: '-quoted strings do NOT interpolate variables:

if(strtotime('$orderdate')
             ^----------^

You're passing the literal characters $, o, r, d, etc... to strtotime(), which is of course not a valid date. So it'll return boolean false, which will be converted to integer 0 for the purposes of your <= comparison.

For a basic "pass a variable as an argument" function call like this, there is absolutely NO reason to use quotes at all:

if(strtotime($orderdate)

And since this is all coming from MySQL, you could do the checking in mysql, saving a few roundtrip type conversion:

SELECT ... (datefield <= (CURDATE() - INTERVAL 52 DAY)) AS older, ...

then you get a simple $row['older'] field which contains a 0 or a 1

Marc B
  • 356,200
  • 43
  • 426
  • 500