これは、データベースのアイテムテーブルにavailable_start
andという名前の2つの列があることを前提としていますavailable_end
。これらは両方ともDATETIMEであると想定されています。
function isItemAvailableNow(item_id){
# Globals are bad but this gets the point across
global $db;
# Get the item from the database
$result = mysql_query("SELECT * FROM items WHERE id = " . mysql_real_escape_string(item_id, $db) . ";");
$row = mysql_fetch_assoc($result);
# Pull the beginning and ending availablity time out of the database result
$available_start = $row['available_start']
$available_end = $row['available_end']
# Get the current time in the same format as the MySQL DATETIME type
$now = date('%Y-%m-%d %H:%i:%s');
return ($now >= $available_start) && ($now <= $available_end);
}
このコードはテストされておらず、私はいくつかの仮定をしました(MySQLを使用しているように)が、これで始めることができます。
タイムゾーンも検討する必要がありますが、それは別の議論です。