Date comparison code problems.

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

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

Date comparison code problems.

Post by akimm »

Ok, my purpose for this code is to read $path variable, enter that directory, view the creation time of the files, if they are lesser than 6 days I want them to echo out with the foreach loop that will then print new articles.

this is the error I'm getting
"Parse error: parse error, unexpected $ in /nfs/cust/8/25/05/650528/web/toop.php on line 21"

toop.php
php:

Code: Select all

<table>
<tr>
<?php
$path = "advertisements/";
if (is_dir("$path"))
       {
           $handle = opendir($path);
           while (false !== ($file = readdir($handle))) {
               if ($file != "." && $file != "..")  { 
                   $Diff = (time() - filectime("$path/$file"))/60/60/24;
                   if ($Diff > 6) {
        foreach($path/$file as $key) {
        echo "<td>" . "new articles" . $key . "</tr>";
                  }
               }
           }
           closedir($handle);
       }
 ?>
</tr></table>
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: Date comparison code problems.

Post by aerodromoi »

akimm wrote:Ok, my purpose for this code is to read $path variable, enter that directory, view the creation time of the files, if they are lesser than 6 days I want them to echo out with the foreach loop that will then print new articles.

this is the error I'm getting
"Parse error: parse error, unexpected $ in /nfs/cust/8/25/05/650528/web/toop.php on line 21"

toop.php
As to the error you mentioned - you didn't close the foreach loop (which you actually don't need at all).

Btw: you won't need a slash between two variables given that the path variable already contains the slash. You might also want to reconsider putting quotes around variables.

Here's the revised code:

Code: Select all

<?php
$path = "img/";
if (is_dir($path))
{   
      $handle = opendir($path);
      
      while ($file = readdir($handle)) {
               if (is_file($path.$file))  {
                   $diff = round((time() - filectime($path.$file))/3600/24);
                   
                   if ($diff > 6) print("new article: " . $file . "<br />\n");
                }
     }
     closedir($handle);
}       
?>
Last edited by aerodromoi on Sun Sep 17, 2006 1:45 am, edited 1 time in total.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Code: Select all

foreach($path/$file as $key) {
The first argument in a foreach construct needs to be an array; it doesn't read directories. Check out http://us2.php.net/manual/en/function.scandir.php
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

I just noticed that you'd like to print out new articles. Then you might want to choose files with a filectime below the six hours threshold - not above ;)
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

ok

Post by akimm »

Yessir.. Thanks for the help, I knew someone on this sight would figure out my mistakes.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: ok

Post by aerodromoi »

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

I have another problem

Post by akimm »

Just along the same lines.

That first script was to read seperate textfiles, but do you know of a way to modify that script to read a textfile and compare the dates on there, thereby echoing the journal title if its within x amount of days. Does anyone know any decent functions that could do this for me?
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: I have another problem

Post by aerodromoi »

akimm wrote:Just along the same lines.

That first script was to read seperate textfiles, but do you know of a way to modify that script to read a textfile and compare the dates on there, thereby echoing the journal title if its within x amount of days. Does anyone know any decent functions that could do this for me?
Sorry, but right now you leave me guessing as to what you actually would like to do...
The first script didn't read any textfiles at all - it simply checked the creation time of all files in a single directory, irrespective of their type.

Anyway, here's a snippet to read the content of a flatfile into an array.

Code: Select all

<?php
$database = "test.txt";
$fp        = fopen ($database ,"r");
$datastr   = fread ($fp,filesize($database));
fclose ($fp);

$dataarr   = split("#",$datastr);
?>
You'll just have to loop through the array and check the current timestamp against the old ones.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Well

Post by akimm »

It didn't go into the .txt file but it did read the creation time, of multiple files.

now however, I have one file that all my journal entrys are written to, basically i have:

Code: Select all

<b>September 13, 2006, 10:48 am</b><font family=arial><BR><BR><b><font color=#006699>Current mood:  </font></b>outlandish<font family=arial><img src=smile/001_9898.gif><br><br><br>  <h1>Paper for Poly Sci</h1><h4>basically this paper is based on the word, Satisficing, its a term in economics that means 'good enough', I was tasked with using this phrase in a sentence, and I achieved that fairly well I think!</h4>As the clock rolled one minute past 4pm this afternoon, I rose from my seated position to greet my Heavyweight teammate named Jake.  He bellowed an informal grunt, which I received happily from Jake, as he wasn’t much for words at any point, and here as practice is set to begin is no different.   	Jake looked dismayed for some reason, but why, I couldn’t gather; I then said to him, “Jake, you doing alright bud, you seem a bit downtrodden”,  to which he quickly shot back, “I’m just fine, classes being hectic as they’ve been, I feel as if I’m teaching myself.” 	Like a Venus flytrap, or a fisherman with a champion bass on the end of a hook, I snapped quickly upwards at a now a bewildered Jake and shot back, “Ah, Yes, I do understand, but Jake you’re very intelligent, wouldn’t you say?” Responding to the compliment Jake was now interesting in what I had been saying, “Yes, I suppose I’m semi-intelligent at least.”“Well Jake, that’s satificing friend, adversus solem ne loquitor , do what you can and everything will pan out as time reveals itself to you.”	Twice over confused Jake responds hesitantly now— “Satis what and what the hell language was that you spoke?”“Satisficing, a term we in the business coined for ‘good enough’, its technical stuff, you know jargon.”  Half smirking so he could see my amusement I then finished, “And that language is an often quoted Latin phrase, it basically means don’t argue obvious stuff.”	Needless to say, Jake walked away, mildly peeved at what he thought was some transgression against his intelligence, I later apologized after practice, explaining to him my intent, and asked if I may narrate  my tale to my political science professor, in order to possibly win some grand prize with this little story we’ve created.  He consented and decided against attacking me, as he may have done had I not reached him in time.  Jake is a very violent fellow.  <font family=arial><BR><BR><HR NOSHADE SIZE=4 COLOR=BLUE><b><font color=#006699>Date posted:  </font></b><b>September 16, 2006, 1:39 am</b><font family=arial><BR><BR><b><font color=#006699>Current mood:  </font></b>apathetic<font family=arial><img src=smile/001_9898.gif><br><br><br>Well well.  twice over was I stuck on an elvator.  However, my second time, (tonight) I had to pee really badly, they actually handed me a bottle to <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> into. 24 oz, filled that sucker up quicklike,  then the nastiest part of the entire sittuation was, when i had to then hold my urine as they rescued me from my elevator like fate.  I was quite literally stuck, and in retrospect, i can say i wish not a third go at the elevator ride, that never ends.  The Resident assistants that helped me out of that mess came and introduced themselves, I was happy to see them face to face, rather than a view from a half-opened door, glaring up, wondering when i'd finally escape my captor.



Through-out the last few days, there has been much hype of my could-be redshirting.  And its actually a possibility, which would be wonderful, allowing me a chance to actually improve, rather than, a straight shot four-years and out.  I think this is a good sittuation for me, I just hope it all pans out for me.  



I'm tired of talking.



BYE<font family=arial><BR><BR><HR NOSHADE SIZE=4 COLOR=BLUE>
I have it fwrite this from a form, I want it to find that date, and then do a date comparison. Any way this is possible?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Here is the entire code

Post by akimm »

journalbox.php (code that writes to file)

Code: Select all

<?php
$filename = "journalaug_sept.txt";
$handle = fopen($filename, 'a');
$write_string = "<b>" . "<font color=#006699>" . "Date posted:  " . "</font>" . "</b>" . $_POST['date']; 
$write_string .= "<font family=arial>"; $write_string .= "<BR>" . "<BR>";
$write_string .= "<b>" . "<font color=#006699>" . "Current mood:  " . "</font>"   . "</b>" . $_POST['mood'];
$write_string .= "<font family=arial>";
$write_string .=  "<img src=" . $_POST['smile'] . ">" . "<br>" . "<br>" . "<br>";
$write_string .= stripslashes($_POST['entry']) . "<font family=arial>" . "<BR>" . "<BR>";
$write_string .= "<HR NOSHADE SIZE=4 COLOR=BLUE>";

fwrite($handle, $write_string);
	fclose($handle);
?>
<h1>Sucessfully posted</h1> 

<br><br>
<?php
echo  "<b>" . "<font color=#006699>" . "Date posted:  " . "</font>" . "</b>" . $_POST['date'] . "<br>";
echo  "<b>" . "<font color=#006699>" . "Current mood:  " . "</font>" . "Current Mood: " . "</b>" . $_POST['mood'] . "<br>";
echo "<img src=" . $_POST['smile'] . ">". "<br>" . "<br>" . "<br>";
echo stripslashes($_POST['entry']) . "<br>";
?>

<br><br>
<a href="http://www.akimm.com/journal06/journal.php">Go To Journal Sean!</a>
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: Here is the entire code

Post by aerodromoi »

I don't have too much time on my hands right now, but if I were you,
I'd try to separate content from formatting. There isn't any sense in storing formatting info
in a flatfile/database as you might need to change it later on.

There are basically two approaches to flatfiles - either you extract the data from a string using regular expressions or
you just separate the individual information with certain tags in order to split the string later on. I'd opt for the latter.
Post Reply