Here's mine:
PHP Code:
<?php
error_reporting(0);
function error($text) {
echo "ERROR: ".$text."<p><a href='".$_SERVER['PHP_SELF']."'>Click here to go back.</a>";
die();
}
?>
<!DOCTYPE html>
<head><title>Tasks</title></head>
<body>
<div style="width: 500px; padding: 20px; border: 1px solid black;">
<?php
echo "Welcome. Today is " . date('l, j F Y') . ".<p>";
if ( $_GET['action'] == "add" ) { // If the user has requested to add a new task
// Parse the parameter and , if possible, save the task
$string = trim($_GET['task']);
if ( empty( $string ) ) {
error("No valid input was found.");
}
if ( strpos( $string, " due " ) === false ){
error("Unable to parse the input.");
}
$date = substr( $string, strrpos($string, " due ") + strlen(" due ") );
$timestamp = strtotime( $date );
if ( $timestamp == -1 || $timestamp === false ) { // PHP's internal could not parse the date, maybe it is the "in N timeunits" format.
if ( strpos( $date, "in " ) !== false ) {
if ( strpos( $date, " day" ) !== false ) {
$word = " day";
$multiplier = 24 * 3600;
} elseif ( strpos( $date, " week" ) !== false ) {
$word = " week";
$multiplier = 7 * 24 * 3600;
} else {
error("Unable to parse the date.");
}
$start = strpos( $date, "in " ) + strlen( "in " );
$length = strpos( $date, $word ) - $start;
$date = trim(substr( $date, $start, $length ));
if ( is_numeric( $date ) && (int)$date == $date && (int)$date > 0 ) {
$date *= $multiplier;
$timestamp = time() + $date;
} else {
error("Unable to parse the date.");
}
} else {
error("Unable to parse the date.");
}
}
$new_task = substr( $string, 0, strrpos($string, " due ") );
$new_task = $new_task . " due on " . date('l, j F Y', $timestamp) . "\n";
echo "New task: " . $new_task;
@ $fp = fopen( "taskfile", "a+" );
if ( !$fp ) {
echo "<p>";
error("Unable to open the taskfile for writing.");
} else {
fputs($fp, $new_task);
echo " has been successfully saved.";
echo "<p><a href='".$_SERVER['PHP_SELF']."'>Click here to go back.</a>";
}
} else { // This is the default action
// Read the file and display saved entries
$tasks = array();
@ $fp = fopen( "taskfile", "r" );
if ( $fp ) {
while ( !feof( $fp ) ) {
$temp = fgets( $fp, 999 );
if ( $temp ) {
$tasks[] = $temp;
}
}
}
echo "You currently have " . count( $tasks ) . " tasks.<p>";
if ( count( $tasks ) ) {
foreach ( $tasks as $key => $value ) {
echo "Task " . $key . ": $value<br>";
}
}
?>
<p>
<div style="padding: 15px; border: 1px dotted gray;">
Please input a new task in the following format:<br><b>[Task Title] due [Date]</b> and click "Submit"<p>
<form method="GET">
<input type="hidden" name="action" value="add">
<input type="text" name="task" style="width: 300px;">
<input type="submit" value="Submit" style="width: 100px;">
</form>
</div>
<?php
}
?>
</div>
</body></html>
Features:
* Saves entries to a file and displays them
* Correctly handles "due" in the title
Sample input:
Code:
foo due now
foo due today
bar due wednesday
bar due tomorrow
foobar due next week
foo bar due next friday
bar foo due 5 dec 2010
bar goo due 5-12-2010
foo due in 2 days
foo due in 2 weeks
Corresponding output:
Code:
foo due on Friday, 3 December 2010
foo due on Friday, 3 December 2010
bar due on Wednesday, 8 December 2010
bar due on Saturday, 4 December 2010
foobar due on Monday, 6 December 2010
foo bar due on Friday, 10 December 2010
bar foo due on Sunday, 5 December 2010
bar goo due on Sunday, 5 December 2010
foo due on Sunday, 5 December 2010
foo due on Friday, 17 December 2010
Peace
Bookmarks