Time comparison with filecttime not evaulating right.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Time comparison with filecttime not evaulating right.

Post by akimm »

Code: Select all

<?php
error_reporting (E_ALL);
$img = "<img src=http://www.akimm.com/images/new1.gif>";
$path = "article/";
$befor2 = fileatime($file)/3600/24;
$roundmok = time()/3600/24;
if(is_dir($path)) {
$handle = opendir($path);
} else {
	die();
}
if($file = readdir($handle)) {
while (is_file($path.$file))  {
			if (($roundmok - $befor2)  >= 10) {
echo "<ul>" . "<li>" . $img .  $file . "<a href=http://www.akimm.com/philosophy.php>" . "     Click to view " . "</a>" . "</li>" . "</ul>";
break;
      } else {
echo "<br />" . "<br />" . "no new articles, add one by" . "<a href=http://www.akimm.com/add_art.php>" . "  clicking here " .

"</a>";
break;
 closedir($handle);
								}
						  	 }
						}
?>
Thanks for anyone who can help!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
echo "what's the script supposed to do?<br />\n";
echo "what is it doing right now?<br />\n";
echo "did you do anything to solve or at least explain the discrepancy between actual and target state?<br />\n";
?>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

I did everything I knew how to solve it, before I posted it. Excuse me for not including more info, I thought I was making a dumb mistake that would be so obvious, as I usually am.

Problem, whenever I include this file into my page, it should echo that a new article was created, because I made a test article. It doesn't echo anything, so somewhere along the code it goes wrong. Not sure why.


Thanks for correcting my mistake about poor description, and poor posting I hope this is better.

Code: Select all

<?php 
error_reporting (E_ALL); 
$img = "<img src=http://www.akimm.com/images/new1.gif>"; 
$path = "article/"; 
#find time of creation of file
$befor2 = fileatime($file)/3600/24;
#time to subtract it by 
$roundmok = time()/3600/24; 
#if the path is a directory
if(is_dir($path)) { 
#establish handle
$handle = opendir($path); 
} else { 
#else stop
        die(); 
} 
#read the hande
if($file = readdir($handle)) { 
#loop through directory
while (is_file($path.$file))  { 
   #if creation time minus todays date is  less than or equal to 10 days print the echo statemtn below                     
if (($roundmok - $befor2)  <= 10) { 
echo "<ul>" . "<li>" . $img .  $file . "<a href=http://www.akimm.com/philosophy.php>" . "     Click to view " . "</a>" . "</li>" . "</ul>"; 
#break the while loop once the procedure is completed
break; 
      } else { 
#else, print this, encouraging someone to add an article.
echo "<br />" . "<br />" . "no new articles, add one by" . "<a href=http://www.akimm.com/add_art.php>" . "  clicking here " . 

"</a>"; 
#break the while loop
break; 
#close the directory
 closedir($handle); 
                                                                } 
                                                         } 
                                                } 
?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

But you still haven't mentionned the problem you're having with the code... Are you expecting to see files that haven't been accessed the last 24 hours and you're not seeing them? (What are the return values for fileatime anyway? Have you read the notes in the manual on this function?)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You probably have files you're testing your script with. And you have expectations. And you get results.
Tell me about it.
akimm wrote:#find time of creation of file
$befor2 = fileatime($file)/3600/24;
What file? What is $file?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

My problem is its not printing that the creation time was less than 10 days, thus its still a new article. It just remains blank. I fileatime evaulates last access of file, so, whenever a file is added will be its last access. This is why I thought I could rely on fileatime.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

I'm not sure where that variable went Volka, somehow I managed to screw it up, I'll be back in a second after I fix that, thanks for noticing.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Could still be anything.
Ok, let's make it concrete, let's print some things.

Code: Select all

<?php
function debugPrint($t, $s) {
	echo '<div>Debug: ', htmlentities($t), '=', htmlentities($s), "</div>\n";
}

error_reporting (E_ALL);
$img = "<img src=http://www.akimm.com/images/new1.gif>";
$path = "article/";
$befor2 = fileatime($file)/3600/24;

debugPrint('img', $img);
debugPrint('path', $path);
debugPrint('file', $file);
debugPrint('befor2', $befor2);

$roundmok = time()/3600/24;
debugPrint('roundmok', $roundmok);

if(is_dir($path)) {
	$handle = opendir($path);
}
else {
	die('not a directory: '.$path);
}

if($file = readdir($handle)) {
	debugPrint('file/readdir', $file);
	while (is_file($path.$file))  {
		debugPrint('$roundmok - $befor2', $roundmok - $befor2);
		if (($roundmok - $befor2)  >= 10) {
			echo "<ul>" . "<li>" . $img .  $file . "<a href=http://www.akimm.com/philosophy.php>" . "     Click to view " . "</a>" . "</li>" . "</ul>";
			break;
		}
		else {
			echo "<br />" . "<br />" . "no new articles, add one by" . "<a href=http://www.akimm.com/add_art.php>" . "  clicking here " . "</a>";
			break;
 			closedir($handle);
		}
	}
}
?>
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Volka, that doesn't print anything, I think I know what I did, when I was editing this, I accidently deleted my $file, so I'm going to fix that now, and then we can see how that will work out.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Volka, that doesn't print anything
Then you made a mistake, as simple as that. It doesn't cause a parse error and it doesn't print nothing.
Last edited by volka on Fri Oct 27, 2006 1:08 pm, edited 1 time in total.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

I added a foreach, to loop through $path, but its not doing the trick still.. there was a file created today, so it should definetly be printed a new article.

Code: Select all

<?php
error_reporting (E_ALL);
$img = "<img src=http://www.akimm.com/images/new1.gif>";
$path = "article/";
foreach(glob($path . "*.txt") as $file) {
#I tried deleteing /3600/24 since I just need fileatime, but its still not doing anything.
$befor2 = filectime($file)/3600/24;
$roundmok = time()/3600/24;
if(is_dir($path)) {
$handle = opendir($path);
} else {
	die();
}
if($file = readdir($handle)) {
while (is_file($path.$file))  {
			if (($roundmok - $befor2)  <= 10) {
echo "<ul>" . "<li>" . $img .  $file . "<a href=http://www.akimm.com/philosophy.php>" . "     Click to view " . "</a>" . "</li>" . "</ul>";
break;
      } else {
echo "<br />" . "<br />" . "no new articles, add one by" . "<a href=http://www.akimm.com/add_art.php>" . "  clicking here " .

"</a>";
break;
 closedir($handle);
								}
						  	 }
						}
					}
?>
Last edited by akimm on Fri Oct 27, 2006 1:13 pm, edited 1 time in total.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Yea Volka, I understand. However, when I was playing with this before, changing different values, it actually was printing an error, a weird error, like it would say files don't exist when they do, I'm reading the correct path, and when it said a file wouldn't exist, it would name the file that does indeed exist. I just wrote terrible code I guess, I'm trying to improve it, but growth for me come as fast as mollases pours.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

a) explain what the script is supposed to do. First what the intention is, then step-by-step, no code!
b) add debug output to your script.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Debug output? As in what my application I write with tells me is wrong? Or my Server?

I want to read through the directory articles/
if a file creation is 10 days young or less I want to tell people a new article is on my page by that echo
if its old, the else statement will tell the person no new articles exist, they should add one.

Did I over complicate?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

filectime() retrieves the time the file last changed, not it's creation time. Are you sure that's what you want?

You may need a clearstatcache() call if you're calling on the file multiple times during the script run.
Post Reply