5-20-2003 hade kanske gått bättre :)
Förstod inte att du ville ha tid som har gått i samma format också.
Då gör vi en lätt modifiering av samma skript så får du bägge delarna.
<?php
function check_date($date) {
// $date i formatet YYYY-MM-DD
$year = substr($date, 0, 4);
$month = (INT)substr($date, 5, 2);
$day = (INT)substr($date, 8, 2);
$check['year'] = date("Y") - $year;
$check['left_year'] = $year - date("Y");
$nToday = date("z");
$nMonth = date("n");
$nDay = date("j");
$nDate = date("z", mktime(1,0,1,$month,$day,date("Y")));
if ($nToday < $nDate){
$check['year']--;
$check['month'] = 12 - $month + $nMonth;
$check['left_month'] = $month - $nMonth;
if ($day > $nDay){
$check['month']--;
$check['days'] = date("z", mktime(1,0,1,$month,$nDay,date("Y"))) - date("z", mktime(1,0,1,$month-1,$day,date("Y")));
$check['left_days'] = $day - $nDay;
} else {
$check['days'] = $nDay - $day;
$check['left_month']--;
$check['left_days'] = date("z", mktime(1,0,1,$month,$day,date("Y"))) - date("z", mktime(1,0,1,$month-1,$nDay,date("Y")));
}
} elseif ($nToday > $nDate) {
$check['month'] = $nMonth - $month;
$check['left_month'] = 12 - $nMonth + $month;
$check['left_year']--;
if ($day > $nDay){
$check['month']--;
$check['days'] = date("z", mktime(1,0,1,$nMonth,$nDay,date("Y"))) - date("z", mktime(1,0,1,$nMonth-1,$day,date("Y")));
$check['left_days'] = $day - $nDay;
} else {
$check['days'] = $nDay - $day;
$check['left_month']--;
if ($month > 1) {
$check['left_days'] = date("z", mktime(1,0,1,$month,$day,date("Y"))) - date("z", mktime(1,0,1,$month-1,$nDay,date("Y")));
} else {
$check['left_days'] = date("z", mktime(1,0,1,$month,$day,date("Y"))) + 1 + date("z", mktime(1,0,1,12,31,date("Y"))) - date("z", mktime(1,0,1,12,$nDay,date("Y")));
}
}
} else {
$check['days'] = 0;
$check['month'] = 0;
$check['left_month'] = 0;
$check['left_days'] = 0;
}
// return $check;
if ($date > date("Y-m-d")) {
return "Det är kvar {$check['left_year']} år, {$check['left_month']} månader och {$check['left_days']} dagar";
} else if ($date < date("Y-m-d")) {
return "Det har gått {$check['year']} år, {$check['month']} månader och {$check['days']} dagar";
} else {
return "Idag är den stora dagen.";
}
} // Slut funktion
echo check_date("2003-05-20");
?>
Hade säkert gått att göra mycket smidigare. Nu finns här stöd för att kunna ta med datum före 1970,
men då hade jag behövt lägga ner lite arbete på det.
Du får själv rensa bort om du vill.