Page 1 of 2

Publishing content based on date.

Posted: Tue Aug 06, 2002 7:20 pm
by WizyWyg
Im trying to figure out a script for a way to publish content when a date is reached.

first off:
Will have to be flat-file based since i have no access to a database.
My php experience is still green, so I follow more by examples so I can understand how it works.

Basically, what I need to know is how to write a script or function so that if I write a posting today, but I dont want it to show up until 7 days from now, how can I get the script to recognize the date (on the server), read from a designated file that contains the update (ex update.txt), then post that content to the site on that date?

So : write today 8/6/02
Will publish on: 8/13/02


Any ideas on where to begin?

Posted: Tue Aug 06, 2002 8:28 pm
by fatalcure
Dunno if this is what your looking for, but should give you a start :)

The way this works is that you have a folder called "Files" with the file names being the date you want them published, in THIS format: "12 August 2002.txt" -- that will be shown on and after that date.

Code: Select all

<?php
	// get directory handle
	$location="Files";
	$hook = opendir($location);
	// read directory and echo list
	while (($file = readdir($hook)) !== false) &#123;
		if ($file != "." && $file != "..") &#123;
			$path = $location . "/" . $file; 		// set up full path
			$file = str_replace(".txt", "", $file);
			$date = time();
			$filedate = strtotime($file) - 86400;
			if ($date >= $filedate) &#123;
               //do your stuff here
					include($path);
			&#125;
		&#125;
	&#125;
	// close directory
	closedir($hook);
?>
hope this helps :)

Posted: Wed Aug 07, 2002 8:48 am
by hob_goblin
better than the one above... just use time() to name it, and then check for 7 day old ones

Posted: Wed Aug 07, 2002 2:06 pm
by WizyWyg
hob_goblin wrote:better than the one above... just use time() to name it, and then check for 7 day old ones
and how would you go about writing the code for it? And how would I set it up so that when the file is created, it will take the time() equivalent for 7 days from now?
the time() function confuses me since it reads time from january 1, 1970. How would you determine what date is what for the file.

Also is there a way so that it saves all to one file?

Posted: Wed Aug 07, 2002 2:22 pm
by WizyWyg
fatalcure wrote:Dunno if this is what your looking for, but should give you a start :)

The way this works is that you have a folder called "Files" with the file names being the date you want them published, in THIS format: "12 August 2002.txt" -- that will be shown on and after that date.

Code: Select all

<?php
	// get directory handle
	$location="Files";
	$hook = opendir($location);
	// read directory and echo list
	while (($file = readdir($hook)) !== false) &#123;
		if ($file != "." && $file != "..") &#123;
			$path = $location . "/" . $file; 		// set up full path
			$file = str_replace(".txt", "", $file);
			$date = time();
			$filedate = strtotime($file) - 86400;
			if ($date >= $filedate) &#123;
               //do your stuff here
					include($path);
			&#125;
		&#125;
	&#125;
	// close directory
	closedir($hook);
?>
hope this helps :)

for this line:
[code$file = str_replace(".txt", "", $file);
$date = time();
$filedate = strtotime($file) - 86400;
if ($date >= $filedate) {
//do your stuff here
include($path);


I can't really follow the function here,
how does
$date = time()
figure into the file name since time() is defined by how much time has gone by since 1/1/1970.
because in your if statement its checking the given $date (given by time()) against the filedate (08august2002.txt for example) and both are not in the same "format". A file name given by time() would be like 1020586922.txt (which is August 5, 2002 at 10 something am)

Posted: Wed Aug 07, 2002 2:38 pm
by WizyWyg
Okay tested the above script out, but

I made a file named 08 august 2002.txt and 08august2002.txt and 8august2002.txt

Did the same for 12 august 2002

All of their content appeared on the page even though today is august 7, 2002. Im testing this locally so I know there isn't a problem with the time on the server.

Any ideas

Posted: Wed Aug 07, 2002 5:34 pm
by WizyWyg
Okay figured out my last posting and now it wont display anything dated 8-9-02 and above (that is if I posted something today 8-7-02).

But, if I have content for say tomorrow (8-8-02) its showing up, even though its 8-7-02.

Posted: Wed Aug 07, 2002 7:07 pm
by fatalcure
code correction:
replace $filedate = strtotime($file) - 86400; with $filedate = strtotime($file);
I know I had a reason for putting the 86400 there, but now I don't know why I did :o

anyway, the way the script works is that it looks at the file names and shows all the files that EQUAL or are BELOW todays date, and includes the file. You can actually do whatever you want (you own code, etc) where it includes it.

To WizyWyg:
strtotime() converts text date formats into UNIX timestamps.

Posted: Thu Aug 08, 2002 2:33 pm
by WizyWyg
Thanks fatalcure, I figured out that 86400 problem (realizing that it had to do with - 24 hours)

Anyway, one mroe question, how can I get it so it lists the Current content first rather than last? Sorting by most recent posted?

Want stuff for 8-08-02 to come before 8-07-02.

Also, I will be posting this to a server that is not based locally ( its -19 hours from our time), how can I make sure it posts at our 12 midnight instead of that servers 12 midnight? (offset from GMT for us is -10 hours)

Posted: Thu Aug 08, 2002 6:36 pm
by fatalcure
Well, to list them in order of most recent, you'd have to store the files in an array first and do a sort on those, then go through the array and find the dates etc.

The way to make sure it appears at 12 midnight our time is to add 19 hours to the time() in the $date;

Code: Select all

$date = time() + 68400;
$filedate = strtotime($file);

Posted: Thu Aug 08, 2002 7:13 pm
by WizyWyg
fatalcure wrote:Well, to list them in order of most recent, you'd have to store the files in an array first and do a sort on those, then go through the array and find the dates etc.
How would you set up that array?

and as for time wouldn't i be subtracting 19 hours since the server is 19 hours ahead?

Posted: Thu Aug 08, 2002 7:52 pm
by fatalcure
Also, I will be posting this to a server that is not based locally ( its -19 hours from our time), how can I make sure it posts at our 12 midnight instead of that servers 12 midnight? (offset from GMT for us is -10 hours)
I thought -19 meant Negative 19....

As for the array, it would be a simple array that would hold the file names.

Look up array on php.net

Posted: Sat Aug 10, 2002 4:25 am
by WizyWyg
fatalcure wrote:
Also, I will be posting this to a server that is not based locally ( its -19 hours from our time), how can I make sure it posts at our 12 midnight instead of that servers 12 midnight? (offset from GMT for us is -10 hours)
I thought -19 meant Negative 19....

As for the array, it would be a simple array that would hold the file names.

Look up array on php.net

But wouldn't that defeat the purpose of having it semi-autmated?
So i wouldnt have to edit the file that the content would show up on.
Since the files are being created on the server (developed the script for that), so the file names are date.txt , how would I set up the array since the file names would become variables.

Posted: Sat Aug 10, 2002 8:04 am
by fatalcure
huh? have u tried to do this at all?

Code: Select all

<?php 
   //setup Array
   myArray = array();

   // get directory handle 
   $location="Files"; 
   $hook = opendir($location); 
   // read directory and echo list 
   while (($file = readdir($hook)) !== false) &#123; 
      if ($file != "." && $file != "..") &#123;
          array_push($myArray, $file);
     &#125;
   &#125;
   // close directory 
   closedir($hook); 

//sort array
rsort($myArray);

//loop through array for each value inside 
for ($i=0; $i <= sizeof($myArray); $i++) &#123;
         $path = $location . "/" . $myArray&#1111;i];       // set up full path 
         $file = str_replace(".txt", "", $myArray&#1111;i]); 
         $date = time(); 
         $filedate = strtotime($file); 
         if ($date >= $filedate) &#123; 
               //do your stuff here 
               include($path); 
         &#125;
&#125;
?>
there :o

Posted: Mon Aug 12, 2002 2:20 pm
by WizyWyg
The code you just posted is returning an error:

Warning: Failed opening 'files/' for inclusion (include_path='.;c:\apache\php\pear') in c:\apache\htdocs\test\test.php on line 37
Which is this line:
include($path);

I think there maybe something to do with the $myArray but I can't figure it out

Also, is there a way to set this same coding up so that it ONLY publishes content for that specific day? (ie Publish only 8-12-2002 content on 8-12-2002 - then when 8-13-2002 comes around only 8-13-2002's content will be shown)


Sorry about before because online tuts and examples dont explain arrays thoroughly. As stated before , I learn by examples. PHP.net didn't offer much on array() as far as examples go.