Jump to content


Photo

New if within 7 days


  • Please log in to reply
56 replies to this topic

#21 pkSML

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

Posted 16 September 2006 - 01:14 PM

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

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

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

Do either of these work?
-Stephen

Need a LitlURL?

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

#22 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 01:18 PM

Ok, well i tried the first code but still no image is shown, the second code gives an error.

An example of the dateadded field is 2006-09-13. This one should work as it is only 3 days old.

Any ideas?

Thanks!!

Peter.

#23 pkSML

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

Posted 16 September 2006 - 01:22 PM

Is the dateadded field separated with dashes or slashes?

What error message did you get previously?
-Stephen

Need a LitlURL?

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

#24 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 01:25 PM

Is the dateadded field separated with dashes or slashes?

Exactly as above, so dashes (2006-09-13)

What error message did you get previously?

For the second code i get this error:

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


Thanks.

Peter.

#25 pkSML

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

Posted 16 September 2006 - 01:34 PM

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

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

-Stephen

Need a LitlURL?

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

#26 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 01:37 PM

When using that code i get this error:

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


Thanks for all your help pkSML

Thanks.

Peter.

**Edit** line 30 is:
if (strtotime("$x['dateadded']") > $sevenDays) {


#27 pkSML

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

Posted 16 September 2006 - 01:39 PM

What's on line 30 of site.php?

BTW, we both moved from nOObie to non-spammers on this forum  ;D

*Edit
I see your edited post.
Change
if (strtotime("$x['dateadded']") > $sevenDays) {
to
if (strtotime($x['dateadded']) > $sevenDays) {

Does this change the output?
-Stephen

Need a LitlURL?

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

#28 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 01:41 PM

woo, not a noobie any more :P

I edited my previous post saying what line 30 was :)

Thanks again.

Peter.

#29 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 01:44 PM

Ah sorry, didn't replace all of the code, thats why i got the error.

It doesn't print anything and doesn't display an image. Any ideas?

Thanks.

Peter.

#30 pkSML

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

Posted 16 September 2006 - 01:50 PM

Try this. I changed some variables around.
If it doesn't work, try the diagnostic version (below). Then show me the output of the diagnostic version.
<?php
date_default_timezone_set("US/Eastern");
while ($x = mysql_fetch_array($result)) {
  list($year, $mon, $day) = explode('-', $x['dateadded']);

  $sevenDays = (7 * 24 * 60 * 60); // 604,800 seconds in 7 days
  $entryTime = strtotime("$year-$mon-$day")
  $now = strtotime("now");

  if (($now - $entryTime) < $sevenDays) {
    // show your image here since it's new!
	echo "<img src='/images/new.gif'>";
  }
}
?>

Diagnostic version:
<?php
date_default_timezone_set("US/Eastern");
while ($x = mysql_fetch_array($result)) {
  list($year, $mon, $day) = explode('-', $x['dateadded']);

  $sevenDays = (7 * 24 * 60 * 60); // 604,800 seconds in 7 days
  $entryTime = strtotime("$year-$mon-$day")
  $now = strtotime("now");

  if (($now - $entryTime) < $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>";
  }
}
?>

-Stephen

Need a LitlURL?

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

#31 pkSML

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

Posted 16 September 2006 - 01:52 PM

Try my last post first. If problems arise, please provide the URL to the site you're working on. It might give me a little more insight to help you.

Also, to make this a lot easier, could you provide FTP access to your site? Then I could see the entire source code and it would be a lot easier to fix. (If you can do this, just email me the info.)
-Stephen

Need a LitlURL?

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

#32 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 01:55 PM

I get this error:

Parse error: parse error, unexpected T_VARIABLE in C:\public_html\RPG\site.php on line 33


Line 33 being:
$now = strtotime("now");

It's all running off a personal web server on my computer at the moment.

#33 pkSML

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

Posted 16 September 2006 - 02:05 PM

Oops, go to line 32. It doesn't have a semicolon ending the line.
-Stephen

Need a LitlURL?

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

#34 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 02:12 PM

:( Still no luck with either of the two codes, just doesn't show anything. I will email you all the source code now.

Thanks.

Peter.

#35 pkSML

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

Posted 16 September 2006 - 02:22 PM

I'll be back in about an hour.
I'm looking at your source code you sent me.

BTW, are the lines 26-50 producing nothing?
-Stephen

Need a LitlURL?

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

#36 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 02:25 PM

Nothing is showing when i run it no.

Thanks.

Pete.

#37 pkSML

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

Posted 16 September 2006 - 05:07 PM

I'm guessing there is a problem accessing the MySQL database properly (note that mysql queries are case-sensitive). The code below should let you know if any info is being passed through mysql_fetch_array. Just throw the code in somewhere in site.php.

Are your other mysql fields showing up in the page? (such as $qry[name], $qry[editorrating], $qry[filesize], etc.)

$result = mysql_query("SELECT * FROM site WHERE id='$id'");
$row = mysql_fetch_array($result) or die(mysql_error());
echo "Date added: ".$row['dateadded'];

The tutorial I'm looking at right now is http://www.tizag.com...lfetcharray.php. The main page is http://www.tizag.com/mysqlTutorial/.

BTW, I'm curious, what server do you use on your home test server? I use the Abyss Web Server.
-Stephen

Need a LitlURL?

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

#38 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 05:38 PM

Yeah, other fields are showing. I put the dataadded query in and it displayed correctly 'Date added: 2006-09-16'.

I'm using apache for my home test server.

Pete.

#39 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 06:00 PM

WOW you guys refuse to make it simple.... LOOK

ALL of these "time in the code below should be stringtotime() prior to running the code

$time = time(); //this determines TODAYS DATE
$time_diff = round(($POST_time - $time)/86400);//this will show how many days diff
if(time_diff,=7){
add image};

#40 wwfc_barmy_army

wwfc_barmy_army
  • Members
  • PipPipPip
  • Advanced Member
  • 320 posts

Posted 16 September 2006 - 06:16 PM

How can i apply this to my system?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users