"Ventsyslav Vassilev" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> Does anybody has an algorithm, which takes 2 different dates (in format
> xxxx-xx-xx xx:xx:xx and date1<date2)
> and returns the difference as:
> "The requested period consists of X years, X months, X weeks, X days, X
> hours, X minutes, X seconds"
>
> For example: With date1='2001-08-09 00:00:00' date2='2001-08-10 01:02:03'
> this code must return:
> "The requested period consists of 0 years, 0 months, 0 weeks, 1 days, 1
> hours, 2 minutes, 3 seconds"
>
> Weeks, days, hours, minutes and seconds can be calculated easily, but
months
> and years?
How's this?
============== date.php ================
<?php
function parse_date($str) {
$t = array();
sscanf($str, "%04d-%02d-%02d %02d:%02d:%02d",
$t["year"], $t["month"], $t["day"], $t["hour"], $t["min"],
$t["sec"] );
return $t;
}
function is_leapyear($year) {
if ($year % 400 == 0)
return true;
if ($year % 100 == 0)
return false;
if ($year % 4 == 0)
return true;
return false;
}
// return b - a
function sub_date($a, $b) {
// assert: $b > $a
$diff = array();
// days per month
$days[1] = 31;
$days[2] = ( is_leapyear($b["year"]) ? 29 : 28 );
$days[3] = 31;
$days[4] = 30;
$days[5] = 31;
$days[6] = 30;
$days[7] = 31;
$days[8] = 31;
$days[9] = 30;
$days[10] = 31;
$days[11] = 30;
$days[12] = 31;
// !allow for wrap!
for ($i = 1; $i <= 12; $i++)
$days[$i-12] = $days[$i];
// do subtraction...
$diff["sec"] = $b["sec"] - $a["sec"];
$diff["min"] = $b["min"] - $a["min"];
$diff["hour"] = $b["hour"] - $a["hour"];
$diff["day"] = $b["day"] - $a["day"];
$diff["month"] = $b["month"] - $a["month"];
$diff["year"] = $b["year"] - $a["year"];
// propagate carries
if ($diff["sec"] < 0) {
$diff["sec"] += 60;
$diff["min"]--;
}
if ($diff["min"] < 0) {
$diff["min"] += 60;
$diff["hour"]--;
}
if ($diff["hour"] < 0) {
$diff["hour"] += 24;
$diff["day"]--;
}
if ($diff["day"] < 0) {
$diff["day"] += $days[$b["month"]-1];
$diff["month"]--;
}
if ($diff["month"] < 0) {
$diff["month"] += 12;
$diff["year"]--;
}
return $diff;
}
function print_date($t) {
$str = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
$t["year"], $t["month"], $t["day"], $t["hour"], $t["min"],
$t["sec"] );
return $str;
}
function alt_print_date($t) {
return (
$t["year"] . " years, "
.$t["month"] . " months, "
.$t["day"] . " days, "
.$t["hour"] . " hours, "
.$t["min"] . " minutes, "
.$t["sec"] . " seconds."
);
}
?>
<html>
<head>
<title>
</title>
</head>
<body>
<form method="post">
<table>
<tr>
<td>First:</td>
<td><input type='text' name='date1' value='<?php echo $date1; ?>'</td>
<td><i>ex: 2001-08-09 00:00:00</i></td>
</tr>
<tr>
<td>Second:</td>
<td><input type='text' name='date2' value='<?php echo $date2; ?>'></td>
<td><i>ex: 2001-08-10 01:02:03</i></td>
</tr>
<tr>
<td><input type='reset'></td>
<td><input type='submit'></td>
</tr>
</table>
</form>
<?php
if (isset($date1) and isset($date2)) {
$d1 = parse_date($date1);
$d2 = parse_date($date2);
$diff = sub_date($d1, $d2);
echo "<br>Difference is ".print_date($diff);
echo "<br>Alternatively, ".alt_print_date($diff);
}
?>
</body>
</html>
======================================
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]