PDA

View Full Version : Date calculation


RobertLees
09-15-2005, 08:05 AM
Hi

Is this possible in html?

I want to show the difference between a certain date, and the current date.

Thanks
Robert

RysChwith
09-15-2005, 08:32 AM
Not in HTML, but you can do it with JavaScript. I'll see if I can get someome to move this to the appropriate forum.

Rys

BonRouge
09-15-2005, 09:21 AM
If you have php available, you can use that.
If you just save this page with a php extension, you can see...
Here's the script in action (http://bonrouge.com/test/timediff.php).
If you need help implementing this, just say.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>The difference between one day and today.</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form method="post" action=""><p><label>Enter the date (dd-mm-yy) : <input name="date1" /></label></p></form>
<?php
function datediff($date1) {

// $date1 is subtracted from $date2.
// if $date2 is not specified, then current date is assumed.

//Splits date apart
list($date1_day, $date1_month, $date1_year) = split('[/.-]', $date1);

if (!$date2) {
$date2_year = date("Y"); //Gets Current Year
$date2_month = date("m"); //Gets Current Month
$date2_day = date("d"); //Gets Current Day
} else {
list($date2_month, $date2_day, $date2_year) = split('[/.-]', $date2);
}

$date1 = mktime(0,0,0,$date1_month, $date1_day, $date1_year); //Gets Unix timestamp for $date1
$date2 = mktime(0,0,0,$date2_month, $date2_day, $date2_year); //Gets Unix timestamp for $date2

$difference = $date2-$date1; //Calcuates Difference
return floor($difference/60/60/24); //Calculates Days Old

}


if (isset($_POST['date1'])) {
$diff = datediff($_POST['date1']);
if ($diff<0) {
echo "<p>$date1 is ".substr($diff,1,10)." days from now.</p>";
}
else {
echo "<p>$date1 was $diff days ago.</p>" ;
}
}
?>
</body>
</html>