<?php
header('Content-Type: text/xml; charset=utf-8');
//=============================================================
$cachefile = '_cache/xml-new.xml';
$cachetime = 60;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
    include($cachefile);
    //echo "\n<!-- generated ".date('M d @ H:i', filemtime($cachefile))." -->";
    exit;
}
ob_start(); // Start the output buffer
//=============================================================
date_default_timezone_set('America/New_York');
include('includes/db.php');
$sitelink = $siteurl . "/";

if ($page > 0) {
	$startnum = ($staticrecords * $page);
	$startnum = $startnum . ",";
}
//nextnum is used for the click for more link.
$nextnum = ($page + 1);

$sterm = GetField($code);
$myquery = "select ID,title,description,blog_date,editorchoice,permalink from " . $tablename . " where status = 1 ORDER by ID DESC limit 111;";

$results = mysqli_query($db,$myquery);
$num = mysqli_num_rows($results);

$feedtitle = "Latest Blogville Blogs";
$feedlink = "http://blogville.us/xml-new.xml";
$feeddescription = "The most recent approved blogs in the blogville blog directory";
$feedtimezone = "EST";
$tm = time () - 3600; 
$tm=date("D, d M Y H:i:s",$tm); // generating the format
$tm=$tm. " $feedtimezone"; // added GMT but you can add your format
//$date->format('Y-m-d H:i:s T')

echo "<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>\n";
echo "<channel>\n";
echo "<atom:link href='$feedlink' rel='self' type='application/rss+xml'/>\n";
echo "<title>" . $feedtitle . "</title>\n";
echo "<link>" . $feedlink . "</link>\n";
echo "<description>" . $feeddescription . "</description>\n";
echo "<managingEditor>" . $siteemail . "(Blogville)</managingEditor>\n";
echo "<generator>$keyword rss creator</generator>\n";
echo "<lastBuildDate>" . $tm . "</lastBuildDate>\n";
echo "<docs>http://blogs.law.harvard.edu/tech/rss</docs>\n";

//-----------------------------------------------------------------------

while ($row = mysqli_fetch_assoc($results)) {
	$ID = $row['ID'];
	$title = strip_tags($row['title']);
	$tm = strtotime($row['blog_date']);
	$t = date("D, d M Y H:i:s",$tm);
	$t = $t ." EST";
	
	$permalink = $row['permalink'];
	
	$titlelen = (strlen($title));
	if ($titlelen > 40) {
		$title = substr($title,0,20);
		$title = $title . "...";
	}

	$description = strip_tags($row['description']);
	$description = stripslashes($description);

	$desclen = (strlen($description));
	if ($desclen > 140) {
		$description = substr($description,0,140);
		$description = $description . "...";
	}

	$shortname = substr($title,0,20);
	echo "<item>\n";
	echo "<title>" . htmlspecialchars($title) . "</title>\n";
	echo "<link>" . $siteurl . "/" . $permalink . "</link>\n";
	echo "<description>" . htmlspecialchars($description) . "</description>\n";
	echo "<author>" . $siteemail . " (blogville.us)</author>\n";
//	echo "<pubDate>" . date(DATE_RFC2822) . "</pubDate>\n";
	echo "<pubDate>" . $t . "</pubDate>\n";
	echo "<guid>\n";
	echo $siteurl . "/" . $permalink;
	echo "\n</guid>\n";
	echo "</item>\n";
}

	echo "</channel>\n";
	echo "</rss>";

mysqli_close($db);

/* The code to dynamically generate the page goes here */
// Cache the output to a file
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush(); // Send the output to the browser
