Skip to main content
JBWebWorks
Inspiring
October 30, 2009
Answered

Need help writing PHP if statement using a date comparison

  • October 30, 2009
  • 2 replies
  • 1349 views

Using PHP/mySQL

have item records with timestamp at insert in column items.item_listed

want to show an image that says 'Item expired', if timestamp is over 60 days ago.

thanks for your help,

Jim Balthrop

This topic has been closed for replies.
Correct answer DwFAQ
what do i change to test for 10 days?

Jim, did you even look at the code?

 $sixty_days = 60 * 24 * 60 * 60;

The code suggests that 60 days * 24 hours in a day * 60 minutes in an hour * 60 seconds in a minute is relative to the time variable. To change it to 10 days you would obviously edit the code like this:

 $sixty_days = 10 * 24 * 60 * 60;

2 replies

David_Powers
Inspiring
October 31, 2009

JBWebWorks wrote:

Using PHP/mySQL

have item records with timestamp at insert in column items.item_listed

What type of timestamp, and which version of MySQL?

MySQL timestamps are not the same as the Unix timestamps used in PHP. Prior to MySQL 4.1, they are in the format YYYYMMDDHHMMSS. In MySQL 4.1 and later, they are formatted as YYYY-MM-DD HH:MM:SS.

Assuming you are storing the date as a MySQL timestamp, the simplest way to handle this is to convert the result to a Unix timestamp for PHP to process.

SELECT UNIX_TIMESTAMP(items.item_listed) AS when_listed

FROM items

You can then use $row_recordsetName['when_listed'] with PHP date and time functions.

function expired($ts) {

  $now = time();

  $sixty_days = 60 * 24 * 60 * 60;

  return $ts+$sixty_days < $now;
}

if (expired($row_recordsetName['when_listed'])) {

  echo '<img src="expired.gif" alt="expired" />';

}

JBWebWorks
Inspiring
October 31, 2009

Thanks for your response, David

i have mySQL version 4.1.20 and TIMESTAMP format is 2009-10-31 09:11:32

Question, i do not have any items over 60 days old yet; what do i change to test for 10 days?

DwFAQ
DwFAQCorrect answer
Participating Frequently
October 31, 2009
what do i change to test for 10 days?

Jim, did you even look at the code?

 $sixty_days = 60 * 24 * 60 * 60;

The code suggests that 60 days * 24 hours in a day * 60 minutes in an hour * 60 seconds in a minute is relative to the time variable. To change it to 10 days you would obviously edit the code like this:

 $sixty_days = 10 * 24 * 60 * 60;
jon8
Inspiring
October 31, 2009

integer strtotime(string time)

The function interprets several standard representations of a date, as shown here:

// Absolute dates and times

$var = strtotime("25 December 2002");

$var = strtotime("14/5/1955");

$var = strtotime("Fr1, 7 Sep 2001 10:28:07 -1000");

// The current time:

$var = strtotime("now");

// Relative times

echo strtotime("+1 day");

echo strtotime("-2 weeks");

echo strtotime("+2 hours 2 seconds");

Care should be taken when using strtotime( ) with user-supplied dates. It's better to limit the use of strtotime( ) to cases when the string to be parsed is under the control of the script, for example, checking a minimum age using a relative date:

// date of birth:

$dob = mktime(0, 0, 0, 16, 8, 1982);

// Now check that the individual is over 18

if ((float)$dob < (float)strtotime("-18 years"))   echo "Legal to drive in the state of Victoria";

In your example, you want to simply add 60 days to the timestamp as it relates to the current date and echo the statement based on if else criteria being met.