include based on current month<

liunx

Guest
I have a sort of online journal thing. Each month, I create a new file named by the year and month: 2003-12

I want the index page to automatically include the current month. I have no idea how to do that. I assume I would need to get the date from the server, extract the month and year from the returned string, then create the file name from these. Could I get some hints on how to go about doing this?you can do this

echo date("Y"); // echos the current year
echo date("m"); // echos the current month
echo date("d"); // echos the current day
echo date("Y-m-d"); // echos the current year/month/dayThis was easier than I thought. Here is what I came up with:
<?php
$today = getdate();
$month = $today['mon'];
$year = $today['year'];

$pathToMainPage = "posts/".$year."-".$month;
$pathTo404Page = "error";

if(!file_exists($pathToMainPage))
{
$month = $month - 1;
$pathToMainPage = "posts/".$year."-".$month;
}

if(isset($_GET["date"]))
{
$pathToIncludePage = "posts/" . $_GET["date"];
if(file_exists($pathToIncludePage))
{
include $pathToIncludePage;
}
else
{
include $pathTo404Page;
}
}
else
{
include $pathToMainPage;
}
?>
This little script will include whatever month is reqested in the URL (index.php?date=2003-05). If there is no month requested, it will include the current month's posts. If the current month does not exist, it will go back one month and post that one. I am assuming that I will never get more than one month behind. If a requested month does not exist, it will include an error page. If anyone has suggestions on making it better, let me know. I hope this can help someone else with simple includes.looks good to me :)
 
Top