Date & Time
|
|
|
|
<HTML><BODY BGCOLOR=FFFFFF>
<?php
//Sets up a Contest to end at a given time.
//For demonstration purposes, this page
//starts a new contest every 2 minutes
//Please change this email...
$notify = 'youremail@yourhost.www';
$connection = pg_connect('host=localhost user=www password=www dbname=contest');
$query = "insert into contestant(entry) values('now()')";
$insert = pg_exec($connection, $query) or die("New Contestant Failed!");
$contestantID = pg_getlastoid($insert);
$contestantID = pg_exec($connection, "select contestantID from contestant where oid = $contestantID") or die("Could not retrieve contestant ID");
$contestantID = pg_result($contestantID, 0, 0);
$query = "select contestantID, entry, contestend from contestant, winner where entry >= contestend limit 1";
$winner = pg_exec($connection, $query) or die("Could not check for winner.");
list($winnerID, $entry, $contestend) = @pg_fetch_row($winner, 0);
if ($winnerID == $contestantID){
echo "You Win!<BR>n";
$message = "$winnerID won the contest ending $contestend at precisely $entryrn";
mail($notify, 'Contest', $message, "From: $notifyrnReply-To: $notifyrn");
$query = "update winner set contestend = datetime_pl_span('$entry', '@ 2 minutes')";
pg_exec($connection, $query) or die("Could not start new contest<BR>n$query");
}
elseif (isset($winnerID) && $winnerID){
//This will only happen in this script for two hits at the exact same time:
echo "Darn! You lost and the contest is over. Fear not, a new one will start soon.<BR>n";
}
else{
echo "Not this time. Try Again!<BR><BR>n";
}
/*
PostgreSQL table definitions:
drop table contestant;
drop sequence contestantID;
create sequence contestantID;
create table contestant(
contestantID int4 default nextval('contestantID'),
entry datetime default('now()')
);
drop table winner;
create table winner(
contestend datetime
);
insert into winner(contestend) values(datetime_pl_span(now(), '@ 120 seconds'));
grant all on contestant to www;
grant all on winner to www;
*/ ?> </BODY></HTML>
<?php exit;?>
|
|
|
Usage Example
|
|
|
Rate This Script
|
|
|
|