Jump to content

New if within 7 days


wwfc_barmy_army

Recommended Posts

Hello.

I am still learning PHP and have been building a site over the last week.

I have done well so far, but i've got to a bit where i can't find an answer anywhere.

Currently i have a date field that is stored like mm-dd-yyyy, i would like to make it so if the date is less then 7 days old then it will display a 'new' image.

I know i need to use an If statement but i'm not 100% sure how to get it to just display it if it's less than 7 days old.

Can anyone give me any advice, links or code?

Thanks.

Peter.  :)
Link to comment
Share on other sites

  • Replies 56
  • Created
  • Last Reply
there's not a very easy way to search by a date field unless you have it in the actual MySQL date datatype of 'yyyy-mm-dd'. as it is, you can probably do what you're after within your loop to display all items. try something like this:
[code]
<?php
// assuming you have queried all your records and your query result is in the $res variable
// set the time 7 days ago
$sevenDays = strtotime('today -7 days');
while ($x = mysql_fetch_array($res)) {
  list($mon, $day, $year) = explode('-', $x['dateField']);
  if (strtotime("$year-$mon-$day") > $sevenDays) {
    // show your image here since it's new!
  }
  // list your item here like normal
}
?>
[/code]

hope this makes sense
Link to comment
Share on other sites

Thanks for the reply.

I tried using that but i get this message:

Strict Standards: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/London' for '1.0/DST' instead in C:\public_html\RPG\site.php on line 27

I'm using this code (includes the changes i made):
[code]<?php
$sevenDays = strtotime('today -7 days');
while ($x = mysql_fetch_array($result)) {
  list($mon, $day, $year) = explode('-', $x['dateadded']);
  if (strtotime("$year-$mon-$day") > $sevenDays) {
    // show your image here since it's new!
echo "<img src='/images/new.gif'>";
  }
}
?>[/code]

Any ideas?

Thanks a LOT in advance!

Cheers.

Pete.
Link to comment
Share on other sites

take youre dates and convert them to stringoftime  subtract the date in question from time() which is the time at that exact moment... what you are left with is the time difference in seconds between now and the date the record has associated to it....  to figure out if it is 7 days divide by 86400 (the number of seconds in a day (ohh that couldbe wrong it might be 84600) and you will end up with a number of days.... if that number is less then or equal to 7 then it happened in the last 7 days.....
Link to comment
Share on other sites

Try this.
[code]<?php
while ($x = mysql_fetch_array($result)) {
  list($mon, $day, $year) = explode('-', $x['dateadded']);
  $day = date('z', mktime(0, 0, 0, $mon, $day, $year)) + 1; // day of year for datestamp
  $today = date("z") + 1; // today's day of year
  if (($today - $day) < 7) {
    // show your image here since it's new!
echo "<img src='/images/new.gif'>";
  }
}
?>[/code]
Link to comment
Share on other sites

This code is fixed. I hope the syntax is proper. date(z) does not need to take into account the month because it just returns the day of year.
This updated code makes sure that the year is the same.

[code]<?php
while ($x = mysql_fetch_array($result)) {
  list($mon, $day, $year) = explode('-', $x['dateadded']);
  $day = date('z', mktime(0, 0, 0, $mon, $day, $year)) + 1; // day of year for datestamp
  $thisyear = date("Y");
  $today = date("z") + 1; // today's day of year
  if ((($today - $day) < 7) && ($year == $thisyear)) {
    // show your image here since it's new!
echo "<img src='/images/new.gif'>";
  }
}
?>[/code]
Link to comment
Share on other sites

as i stated earlier the code you keep trying to push is wrong because it just wont work... what happens when something is posted on the 31st and today is the 1st?  you do need to account for days months and years so you need to use the stringtotime... why you both refuse to accept this fact is beyond me... its fast simple and less lines of code
Link to comment
Share on other sites

[quote author=pkSML link=topic=108166.msg435291#msg435291 date=1158368531]
This code is fixed. I hope the syntax is proper. date(z) does not need to take into account the month because it just returns the day of year.
This updated code makes sure that the year is the same.

[code]<?php
while ($x = mysql_fetch_array($result)) {
  list($mon, $day, $year) = explode('-', $x['dateadded']);
  $day = date('z', mktime(0, 0, 0, $mon, $day, $year)) + 1; // day of year for datestamp
  $thisyear = date("Y");
  $today = date("z") + 1; // today's day of year
  if ((($today - $day) < 7) && ($year == $thisyear)) {
    // show your image here since it's new!
echo "<img src='/images/new.gif'>";
  }
}
?>[/code]
[/quote]

I ran this, but it doesn't show anything even though the selected record has the dateadded as 3 days ago.

Any ideas?

Thanks.

Peter
Link to comment
Share on other sites

wwfc_barmy_army, could you provide an example record of $mon, $day, $year?

What I'm thinking: Maybe SQL returns $year as 06 instead of 2006. This would cause the script to never show the NEW graphic. If this is the case, just change the line [b]$thisyear = date("Y");[/b] to [b]$thisyear = date("y");[/b] (just change capital Y to little y).
Link to comment
Share on other sites

OK, let's try fixing the original code (hope you don't mind :)).
The problem was occurring with the timezone function. Let's set the timezone.
[code]<?php
date_default_timezone_set("US/Eastern");
$sevenDays = strtotime('today -7 days');
while ($x = mysql_fetch_array($result)) {
  list($mon, $day, $year) = explode('-', $x['dateadded']);
  if (strtotime("$year-$mon-$day") > $sevenDays) {
    // show your image here since it's new!
echo "<img src='/images/new.gif'>";
  }
}
?>[/code]
My timezone is Eastern, but you can find your timezone at [url=http://us2.php.net/manual/en/timezones.america.php]http://us2.php.net/manual/en/timezones.america.php[/url] if you live in America. Otherwise, find it at [url=http://us2.php.net/manual/en/timezones.php]http://us2.php.net/manual/en/timezones.php[/url].

[b]Edit:[/b]
About the previous posts, markbett is right: strtotime is better to use than day of year. The only problem that occurs with day of year is when it's time to change years (you'd not see the new graphic for the first seven days of a new year).
Link to comment
Share on other sites

OK, that makes a huge difference and explains why it wasn't working right.

[code]<?php
date_default_timezone_set("US/Eastern");
$sevenDays = strtotime('today -7 days');
while ($x = mysql_fetch_array($result)) {
  list($year, $mon, $day) = explode('-', $x['dateadded']);
  if (strtotime("$year-$mon-$day") > $sevenDays) {
    // show your image here since it's new!
echo "<img src='/images/new.gif'>";
  }
}
?>[/code]

BTW, the variables $year, $mon, and $day come from $x['dateadded']. The line that begins with list() basically splits the string at every dash mark (using the explode function), and assigns the variables in the order of the list.

You might even be able to use more concise code:
[code]<?php
date_default_timezone_set("US/Eastern");
$sevenDays = strtotime('today -7 days');
while ($x = mysql_fetch_array($result)) {
  if (strtotime("$x['dateadded']") > $sevenDays) {
    // show your image here since it's new!
echo "<img src='/images/new.gif'>";
  }
}
?>[/code]

Do either of these work?
Link to comment
Share on other sites

[quote author=pkSML link=topic=108166.msg435488#msg435488 date=1158412926]
Is the dateadded field separated with dashes or slashes?[/quote]
Exactly as above, so dashes (2006-09-13)

[quote]
What error message did you get previously?
[/quote]
For the [b]second[/b] code i get this error:
[quote]
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\public_html\RPG\site.php on line 30
[/quote]

Thanks.

Peter.
Link to comment
Share on other sites

For diagnostic's sake, give me the output of this code. It just lists the variables and their values.

[code]<?php
date_default_timezone_set("US/Eastern");
$sevenDays = strtotime('today -7 days');
while ($x = mysql_fetch_array($result)) {
  list($year, $mon, $day) = explode('-', $x['dateadded']);
  if (strtotime("$year-$mon-$day") > $sevenDays) {
    // show your image here since it's new!
echo "<img src='/images/new.gif'>";
  }
  else {
  print "<PRE>";
  print '$year = '.$year."\n";
  print '$mon = '.$mon."\n";
  print '$day = '.$day."\n";
  print "\$x['dateadded'] = ".$x['dateadded']."\n";
  print '$sevenDays = '.$sevenDays."\n";
  print 'strtotime("$year-$mon-$day") = '.strtotime("$year-$mon-$day")."\n";
  print "</PRE>";
  }
}
?>[/code]
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.