Hello,

I have a virtual pet website that has a soup kitchen which should only open at certain times during the day!

However, I want to be able to add more timings to the kitchen opening hours but for some reason the script doesnt execute the IF statement and therefore doesnt open at the times listed in the script. I have tried all sorts and cant seem to work it out! Im not sure if it is the coding or if its something to do with my server clock or my community PHP clock coding. :O(

Upon visiting during the opening hours (the IF statement), the soup kitchen is always closed and goes to the else statement.

Any obvious errors that I have over looked? I have put the piece of code and then have put the page code below.

Any help is much appreciated!

Justin

if ((($hour >= 01) AND ($hour <= 08)) OR (($hour >= 11) AND ($hour <= 13)) OR (($hour >= 18) AND ($hour <= 19)) AND (!$findCheck[id]))
	{
		print "<p align=center><center><a href=free_food.php?act=feedme&game=$game>Get your Free Meal!</a></p>";
	}
	else
	{
		print "<p align=center><b>Sorry, we're closed now, please come back during serving hours.</b></p>";
	}
}

PAGE CODE:

<?php

/*

Free Food (free_food.php)

*/

$page_title = "Free Food";
$rank_check = 1;
include "header.inc.php";

print "$openHTML<br>";

// Start Check

$checking = "freefood";
$limit1 = 7200;
$limit = $timestamp - $limit1;

$findCheck = fetch("SELECT * FROM checking2 WHERE userid = '$userid' AND check_what = '$checking' AND timestamp > '$limit'");

// End Check

if ($act == "feedme")
{
	if ($findCheck[id])
	{
		die("<p><center>You can only get free food ONCE per serving time. <a href=$base_url/free_food.php?game=$game>Back</a></p>$closeHTML");
	}

	$randHunger = rand(1,4);

	mysql_query("UPDATE members2 SET hunger_level = hunger_level + $randHunger WHERE username = '$username' AND game = '$game'");

	mysql_query("UPDATE user_pets2 SET hunger = hunger + $randHunger WHERE owner = '$userid' AND game = '$game'");

	mysql_query("UPDATE members2 SET hunger_level = 10 WHERE username = '$username' AND game = '$game' AND hunger_level > '10'");

	mysql_query("UPDATE user_pets2 SET hunger = 10 WHERE owner = '$userid' AND game = '$game' AND hunger > '10'");

	if ($randHunger == "1") { $pageTitle = "Slim Pickings"; }
	if ($randHunger == "2") { $pageTitle = "Satisfying Meal"; }
	if ($randHunger == "3") { $pageTitle = "3 Course Meal"; }
	if ($randHunger == "4")
	{
		$pageTitle = "Feasting!";

		mysql_query("UPDATE members2 SET hunger_level = 10 WHERE username = '$username' AND game = '$game'");

		mysql_query("UPDATE user_pets2 SET hunger = 10 WHERE owner = '$userid' AND game = '$game'");
	}

	mysql_query("DELETE FROM checking2 WHERE userid = '$userid' AND check_what = '$checking'");
	mysql_query("INSERT INTO checking2 (userid,check_what,timestamp,game) VALUES ('$userid','$checking','$timestamp','$game')");

	$findPage = fetch("SELECT * FROM game_pages WHERE page_title = '$pageTitle' AND game = '$game'");
	if (!$findPage[id])
	{
		if ($randHunger == "1") { print "<p><center>Sorry, but hardly anyone in the community has been donating, we only have left overs from yesterdays meal.</p>"; }
		if ($randHunger == "2") { print "<p><center>Here you go, the main course and an ice cold drink!</p>"; }
		if ($randHunger == "3") { print "<p><center>Quite a few people in the community have been donating! You get a full three course meal!!</p>"; }
		if ($randHunger == "4") { print "<p><center>WOW! A big food drive has just been completed, you FEAST on food and you and your pets are all full!</p>"; }
	}
	else
	{
		print "$findPage[page_info]";
	}
}

if ($act != "feedme")
{

	$findPage = fetch("SELECT * FROM game_pages WHERE page_title = '$page_title' AND game = '$game'");
	if (!$findPage[id])
	{
		print "<p><center>Welcome to the Soup Kitcen, we serve food to the needy every day. So put your pride aside and please take some food :)</p>
		<p>Business hours:<Br>
		7 AM to 8 AM GMT <br>
		11 AM to 1 PM GMT and<Br>
		6 PM to 7 PM GMT </p>
<p><br> These hours do sometimes change depending on our volunteers so keep coming back if we dont happen to be open... Thank you! :O)
		";
	}
	else
	{
		print "$findPage[page_info]";
	}

	if ((($hour >= 01) AND ($hour <= 08)) OR (($hour >= 11) AND ($hour <= 13)) OR (($hour >= 18) AND ($hour <= 19)) AND (!$findCheck[id]))
	{
		print "<p align=center><center><a href=free_food.php?act=feedme&game=$game>Get your Free Meal!</a></p>";
	}
	else
	{
		print "<p align=center><b>Sorry, we're closed now, please come back during serving hours.</b></p>";
	}
}

print "$closeHTML";
?>

I'm sorry but I don't see where you are setting $hour. If $hour is not set then this will never fire.

maybe you should include this somewhere:

$hour = date("G");

I have the $hour set in the globals.inc.php which is included in the header ...

This is part of the globals.inc.php :>/

$date = date("U"); // TIMESTAMP
$timestamp = $date;
$minute = date("i", $date);        // MINUTE 00-59
$hour = date("H", $date);          // HOUR 00-23
$hour2 = date("h", $date);          // HOUR 00-12
$today = date("j",$date);          // DAY 1-31
$today2 = date("d",$date);         // DAY 01-31
$today3 = date("w",$date) + 1;     // DAY 0 (sunday) - 6 (saturday) + 1
$this_week = date("W",$date);      // WEEK 0-52
$this_month = date("n",$date);     // MONTH 1-12
$this_month2 = date("m",$date);    // MONTH 01-12
$this_year = date("Y",$date);      // YEAR YYYY
$datestamp = date("Ymd",$date);    // DATE STAMP YYYYmmdd
$timestamp2 = date("H:i:s",$date); // TIME STAMP HH:ii:ss - HOUR:MINUTE:SECOND
$datestamp2 = date("M j",$date);   // DATE STAMP Mon DD
$datestamp3 = date("m-j-",$date);  // DATE STAMP mm-jj-
$datestamp4 = date("m-d-",$date);  // DATE STAMP mm-dd-

Thank you for your time
Justin

that only leaves one option and that is this line here:

$findCheck = fetch("SELECT * FROM checking2 WHERE userid = '$userid' AND check_what = '$checking' AND timestamp > '$limit'");

where is your "fetch()" function, "echo" the query and paste it into your database manager to verify that it does get some data.

Member Avatar for langsor

Having recently played with the PHP date object, helping another person out, I discovered that when comparing the date values against each other or against a number -- you need to convert the date output to a number also.

Just a guess -- try this

$hour = $hour + 0;

if ((($hour >= 01) AND ($hour <= 08)) OR (($hour >= 11) AND ($hour <= 13)) OR (($hour >= 18) AND ($hour <= 19)) AND (!$findCheck[id]))
  {
    print "<p align=center><center><a href=free_food.php?act=feedme&game=$game>Get your Free Meal!</a></p>";
  }
  else
  {
    print "<p align=center><b>Sorry, we're closed now, please come back during serving hours.</b></p>";
  }
}

What that does is truncate any non-numeric values off of $hour, but the way he is using it shouldn't matter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.