Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Need help writing PHP if statement using a date comparison

Participant ,
Oct 30, 2009 Oct 30, 2009

Copy link to clipboard

Copied

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

TOPICS
Server side applications

Views

1.2K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advisor , Oct 31, 2009 Oct 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;

Votes

Translate
Enthusiast ,
Oct 31, 2009 Oct 31, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 31, 2009 Oct 31, 2009

Copy link to clipboard

Copied

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" />';

}

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 31, 2009 Oct 31, 2009

Copy link to clipboard

Copied

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?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 31, 2009 Oct 31, 2009

Copy link to clipboard

Copied

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;

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 31, 2009 Oct 31, 2009

Copy link to clipboard

Copied

thanks DwFAQ,

that is what i thought but i have not worked with date functions much, so i am trying to learn

so 60 days would produce 5184000 seconds

my TIMESTAMP is producing 1256573150  for a date format of 2009-10-26 11:05:50

time() produces 1257019909

so testing for 6 days

1256573150(timestamp) + 51840(6 days) is not less than 1257019909(now time) so the EXPIRED image will not show

thanks David for your help

Jim Balthrop

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 31, 2009 Oct 31, 2009

Copy link to clipboard

Copied

LATEST

David,

Using your example, i got my page to function exactly as i needed.

An item will expire, an 'item expired' image will show along with an update button to repost the same item by updating the TIMESTAMP.

I went back to your book, 'Dreamweaver CS3 with CSS, Ajax, and PHP and did find your reference to generating a Unix timestamp on page 610 but never would have known how it related to my situation. Thanks to you for participating in these forums.

Jim Balthrop

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines