<?php
$timestamp = time();

// Connect
 $conn = mysql_connect("localhost", "birdstrike", "dookee") or die(mysql_error());
 $db = mysql_select_db("birdstrike", $conn) or die(mysql_error());
 
// Get date
$text_date = date("m-d-Y", $timestamp);
$cart_text_date = explode("-", $text_date);
$month = "$cart_text_date[0]";
$day = "$cart_text_date[1]";
$year = "$cart_text_date[2]";
$midnight_timestamp = mktime(0, 0, 0, $month, $day, $year);
 
$temp_timestamp = $midnight_timestamp;

for ($day=0; $day < 4; $day++) {// run through and get jokes for past three days
// clear var
$jotd = "";
$rdate = "";
$vdate = "";
// Get Joke
$sql_joke = "Select joke from joke_of_the_day where post_timestamp = '$temp_timestamp'";
$result_joke = mysql_query ($sql_joke, $conn) or die(mysql_error());
$cart_joke = mysql_fetch_array($result_joke);
// Make var
$jotd = str_replace("<br>", "", $cart_joke[0]);
$jotd = str_replace("", "", $jotd);
// make date
$rdate = date("r", ($temp_timestamp+60));
$vdate = date("l, jS of M. Y", ($temp_timestamp+60));

if ($jotd != "") {
$joke .= 
"<item>
<title>Joke of the Day for: $vdate</title>
<description>$jotd</description>
<link>http://birdstriketheatre.myftp.biz/calendar.php</link>
<pubDate>$rdate</pubDate>
<guid>http://birdstriketheatre.myftp.biz/calendar_jotd_popup.php?$temp_timestamp</guid>
</item>";
	} // end if
	// add time to temp_timestamp
	$temp_timestamp = $temp_timestamp - 24*60*60;
} // end for day

// Get up coming shows
$timestamp_m_2h = $timestamp - 2*60*60;
$timestamp_p_24h = $timestamp + 24*60*60;
$timestamp_p_7d = $timestamp + 7*24*60*60;
$timestamp_p_m = $timestamp + 31*24*60*60;
// check within day
$sql_row_d = "Select timestamp from gigs where timestamp > $timestamp_m_2h and timestamp < $timestamp_p_24h";
$result_row_d = mysql_query ($sql_row_d, $conn) or die(mysql_error());
$cart_timestamp_d =  mysql_fetch_array($result_row_d);
// check within week
$sql_row_w = "Select timestamp from gigs where timestamp > $timestamp_p_24h and timestamp < $timestamp_p_7d";
$result_row_w = mysql_query ($sql_row_w, $conn) or die(mysql_error());
$cart_timestamp_w =  mysql_fetch_array($result_row_w);
// check within month
$sql_row_m = "Select timestamp from gigs where timestamp > $timestamp_p_7d and timestamp < $timestamp_p_m";
$result_row_m = mysql_query ($sql_row_m, $conn) or die(mysql_error());
$cart_timestamp_m =  mysql_fetch_array($result_row_m);

$shows = "";
if (($cart_timestamp_d[0] != "") or (($cart_timestamp_w[0] !="") or ($cart_timestamp_m[0] != ""))){

	if($cart_timestamp_d[0] != "") { // if in 24 hrs
		foreach($cart_timestamp_d as $show_timestamp) {
			// clear varibales
			$show_name = "";
			$show_location = "";
			$rdate = "";
			$vdate = "";
			$vtime = "";
			// read db
			$sql_show = "Select name, location from gigs where timestamp = '$show_timestamp'";
			$result_show = mysql_query ($sql_show, $conn) or die(mysql_error());
			$cart_show =  mysql_fetch_array($result_show);
			// Make variables
			$show_name = $cart_show[0];
			$show_location = $cart_show[1];
			$rdate = date("r", ($show_timestamp - 31*24*60*60));
			$vdate = date("l, jS of M. Y", $show_timestamp);
			$vtime = date("h:i a", $show_timestamp);
			// post var
			$shows .= 
			"<item>
			<title>Upcoming Show: $show_name on $vdate </title>
			<description>What: $show_name Where: $show_location When: $vdate @ $vtime</description>
			<link>http://birdstriketheatre.myftp.biz/view_show.php?$show_timestamp</link>
			<pubDate>$rdate</pubDate>
			<guid>http://birdstriketheatre.myftp.biz/</guid>
			</item>";
			break;
		}
	} // end if
	
	if ($cart_timestamp_w[0] !="") { // else if in week
		foreach($cart_timestamp_w as $show_timestamp) {
			// clear varibales
			$show_name = "";
			$show_location = "";
			$rdate = "";
			$vdate = "";
			$vtime = "";
			// read db
			$sql_show = "Select name, location from gigs where timestamp = '$show_timestamp'";
			$result_show = mysql_query ($sql_show, $conn) or die(mysql_error());
			$cart_show =  mysql_fetch_array($result_show);
			// Make variables
			$show_name = $cart_show[0];
			$show_location = $cart_show[1];
			$rdate = date("r", ($show_timestamp - 31*24*60*60));
			$vdate = date("l, jS of M. Y", $show_timestamp);
			$vtime = date("h:i a", $show_timestamp);
			// post var
			$shows .= 
			"<item>
			<title>Upcoming Show: $show_name on $vdate </title>
			<description>What: $show_name Where: $show_location When: $vdate @ $vtime</description>
			<link>http://birdstriketheatre.myftp.biz/view_show.php?$show_timestamp</link>
			<pubDate>$rdate</pubDate>
			<guid>http://birdstriketheatre.myftp.biz/</guid>
			</item>";
			break;
		}
	} // end if

	if ($cart_timestamp_m[0] !="") { // else if in month
		foreach($cart_timestamp_m as $show_timestamp) {
			// clear varibales
			$show_name = "";
			$show_location = "";
			$rdate = "";
			$vdate = "";
			$vtime = "";
			// read db
			$sql_show = "Select name, location from gigs where timestamp = '$show_timestamp'";
			$result_show = mysql_query ($sql_show, $conn) or die(mysql_error());
			$cart_show =  mysql_fetch_array($result_show);
			// Make variables
			$show_name = $cart_show[0];
			$show_location = $cart_show[1];
			$rdate = date("r", ($show_timestamp - 31*24*60*60));
			$vdate = date("l, jS of M. Y", $show_timestamp);
			$vtime = date("h:i a", $show_timestamp);
			// post var
			$shows .= 
			"<item>
			<title>Upcoming Show: $show_name on $vdate </title>
			<description>What: $show_name Where: $show_location When: $vdate @ $vtime</description>
			<link>http://birdstriketheatre.myftp.biz/view_show.php?$show_timestamp</link>
			<pubDate>$rdate</pubDate>
			<guid>http://birdstriketheatre.myftp.biz/</guid>
			</item>";
			break;
		}
	} // end if
} else { // no upcoming shows
			$shows .= 
			"<item>
			<title>Upcoming Show: Non right now. Sorry. </title>
			<description></description>
			<link>http://birdstriketheatre.myftp.biz/</link>
			<pubDate>".date("r", $timestamp)."</pubDate>
			<guid>http://birdstriketheatre.myftp.biz/</guid>
			</item>";
}// end if in next month
?>
<?php header("Content-type: text/xml"); ?>
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Birdstrike Theatre</title>
<link>http://www.birdstriketheatre.com/</link>
<description>We're funny people.</description>
<copyright>Copyright 2006 Birdstrike Theatre</copyright>
<language>en-us</language>
<image>
<url>http://birdstriketheatre.myftp.biz/favicon.gif</url>
<title>Birdstrike Theatre.com</title>
<link>http://www.birdstriketheatre.com/</link>
</image>
<?php echo $joke; ?>
<?php echo $shows; ?>
<ttl>1460</ttl>
</channel>
</rss>