Dynadot โ€” .com Registration $8.99

Need a special PHP 'include'

Spaceship Spaceship
Watch

Gene

Gene PimentelTop Member
Impact
485
Let's say I have hundreds of HTML files, each named for a particular day of the year.

I need to use the PHP 'include' code so on any particular system date, the matching HTML file is included. So, since today is August 27, 2008, the 'include' file should be 2008-08-27.html

Understand that the html files have not been created yet, so the naming of these files can be in a different date format if need be.

------------------------

Then I need to have another 'include' code that will take the system date and subtract a number of days, so it will 'include' a HTML file that is X number of days in the past.

For example, today is August 27, 2008, and I want the web page to 'include' a file named for a date 3 days ago, which would be 2008-08-24.html.

------------------------

Seems like a fairly simple thing to do, but I don't know coding well enough to figure it out. Any help would be great!




.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
Well the part of it to include "Today" could be :

Code:
<?php

$today = date("Ymd");

include("includes/$today.php");

?>

That would actually show the date as "20080827" - And as a whole number you could simply Subtract numbers from that to show archives of days past.

Code:
$yesterday = $today-1;


That may be a bit too simple for what you are looking for Gene - But it's 8 AM here and I'm out of Mountain Dew :laugh:

SOS DEW ME !

Obviously - That is limited to one month though ... :|
 
0
•••
Thanks Mark!
That seems like it would work (based on my limited understanding of PHP), but I'm wondering why you say it would be limited to 1 month. Couldn't it say $yesterday = $today-47; for instance?


So do I have this right?

PHP:
<?php

$today = date("Ymd");
$yesterday = $today-1;
include("includes/$today.php");

?>

(which would use file: 20080827.php)



OR



<?php

$today = date("Ymd");
$yesterday = $today-3;
include("includes/$yesterday.php");

?>

(which would use file: 20080824.php)


----------------------------
EDIT: Mark, you're da man! I just tried the code and it works perfectly. THANK YOU!





.
 
Last edited:
0
•••
No - it wont work for other months due to the fact it is combining Y M D into one number. "20080827" is just "20 Million 80 thousand and 27" to the script. If you subtract more than 26 , There would be an error if you tried to incude any pages.

For individual dates , You really need a more detailed Calendar set up and something in the script to ask "if file exists" to make sure it doesn't try to include dates non-existent. http://www.php.net/date has all of the available formats. I'm thinking in your case maybe using the "Day of Year" format may help.
z The day of the year (starting from 0) 0 through 365

Here is a piece of a script I use to pull up "Todays Database" :

Code:
$today = date("Ymd");
if(!isset($filename) || !$filename || $filename == ""){$filename = "database/$today.txt";}
	if(!file_exists("$filename")) {

///it does exist - so retrieve and show data (include it in your case)

} else {

///it doesn't exist - So no Go (I actually create the database here on my script)

}

I'll do some more thinking and digging on it after a Trip to the store (Mountain Dew Trip) :laugh:

I'd bet some of these younger "whippersnappers" might have a better way to do this than me :guilty:
 
0
•••
You're right... I just tried doing a -30 and it failed... it won't overlap into the previous month. Thanks for the additional info though!
 
0
•••
The strtotime function is almost unbelievably clever at working out date stuff, you can even tell it what to do in almost plain english:

Code:
$yesterday = date('Ymd', strtotime("-1 day"));
$lastmonth = date('Ymd', strtotime("-1 month"));

check out some of the more extreme examples:
Code:
strtotime("last Monday")
strtotime("next Thursday")
http://php.net/strtotime
 
0
•••
-NC-! Beautiful! That works like a charm... man you've saved me a lot of hair-pulling. Thanks so much.
 
0
•••
:laugh: I told you one of those Younger folks would know a better way ;)

Thanks -NC- :tu:
 
0
•••
Thanks to both of you for taking the time! And Mark, you're still the best eye-poker ;)
 
0
•••
Good call, glad I read this thing through instead of just posting like normal then I would've made a double post xD good job -NC-

-RageD
 
0
•••
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back