Jump to content


Photo

New if within 7 days


  • Please log in to reply
56 replies to this topic

#1 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 15 September 2006 - 12:26 PM

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.  :)

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 15 September 2006 - 12:38 PM

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:
<?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
}
?>

hope this makes sense
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 15 September 2006 - 11:07 PM

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):
<?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'>";
  }
}
?>

Any ideas?

Thanks a LOT in advance!

Cheers.

Pete.


#4 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 15 September 2006 - 11:29 PM

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.....

#5 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 15 September 2006 - 11:57 PM

Hello.

I dont' quite understand what you are saying. What can i do with the code that i have already?

Thanks.

Peter.

#6 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 12:01 AM

$time = time();
$time_diff = round(($stop_time-$time)/86400);

compare time_diff to 7

#7 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 12:03 AM

Will this work even though the date is stored (using CURDATE()) into the database as MM/DD/YYYY?

#8 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 12:04 AM

you need to convert all your date to string of time like i said

#9 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 12:07 AM

I am not sure how to do this, like i said i'm reasonable new to PHP, i currently use 'CURDATE()' to add the date to the database, how do i change it to use as a string of time?

#10 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 12:10 AM

http://www.w3schools...e_strtotime.asp

#11 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 12:23 AM

No offence but this doesn't help someone new to php like me. Could anyone else help me out a bit more?

Thanks.

Pete.

#12 pkSML

pkSML
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationBucyrus, Ohio

Posted 16 September 2006 - 12:46 AM

Try this.
<?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'>";
  }
}
?>

-Stephen

Need a LitlURL?

Got code to share? --> The Code-Bin

#13 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 12:51 AM

which works great for looking at how far apart DAYS are but you dont take into account the year or month...

so in your code comparing jan 3 with june 5 would result in true when it should be false which is why you should compare them as unixtime

#14 pkSML

pkSML
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationBucyrus, Ohio

Posted 16 September 2006 - 01:02 AM

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.

<?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'>";
  }
}
?>

-Stephen

Need a LitlURL?

Got code to share? --> The Code-Bin

#15 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 05:26 AM

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

#16 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 08:59 AM

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.

<?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'>";
  }
}
?>


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

#17 pkSML

pkSML
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationBucyrus, Ohio

Posted 16 September 2006 - 12:20 PM

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 $thisyear = date("Y"); to $thisyear = date("y"); (just change capital Y to little y).
-Stephen

Need a LitlURL?

Got code to share? --> The Code-Bin

#18 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 12:46 PM

I tried changing the Y to y but it still doesn't show anything.

I don't know where '$mon, $day, $year' is coming from.

#19 pkSML

pkSML
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationBucyrus, Ohio

Posted 16 September 2006 - 12:53 PM

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.
<?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'>";
  }
}
?>
My timezone is Eastern, but you can find your timezone at http://us2.php.net/m...nes.america.php if you live in America. Otherwise, find it at http://us2.php.net/m...n/timezones.php.

Edit:
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).
-Stephen

Need a LitlURL?

Got code to share? --> The Code-Bin

#20 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 01:04 PM

I don't mind :P

I've put that in, but like the last even though the dateadded is less then 7 days ago it doesn't show the image.

I've just realised that the date is shown like this: 2006-09-16 , i orginally thought it was different.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users