tempo fai trovai come open source la funzione DateDiff dell'ASP convertita in PHP per scopi didattici

Codice PHP:
function datediff($interval$date1$date2) {
    
$seconds $date2 $date1;
    switch (
$interval) {
        case 
"y":    // years
            
list($year1$month1$day1) = split('-'date('Y-m-d'$date1));
            list(
$year2$month2$day2) = split('-'date('Y-m-d'$date2));
            
$time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) + (date('s',$date1));
            
$time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) + (date('s',$date2));
            
$diff $year2 $year1;
            if(
$month1 $month2) {
                
$diff -= 1;
            } elseif(
$month1 == $month2) {
                if(
$day1 $day2) {
                    
$diff -= 1;
                } elseif(
$day1 == $day2) {
                    if(
$time1 $time2) {
                        
$diff -= 1;
                    }
                }
            }
            break;
        case 
"m":    // months
            
list($year1$month1$day1) = split('-'date('Y-m-d'$date1));
            list(
$year2$month2$day2) = split('-'date('Y-m-d'$date2));
            
$time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) + (date('s',$date1));
            
$time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) + (date('s',$date2));
            
$diff = ($year2 12 $month2) - ($year1 12 $month1);
            if(
$day1 $day2) {
                
$diff -= 1;
            } elseif(
$day1 == $day2) {
                if(
$time1 $time2) {
                    
$diff -= 1;
                }
            }
            break;
       case 
"w":    // weeks
            // Only simple seconds calculation needed from here on
            
$diff floor($seconds 604800);
            break;
       case 
"d":    // days
            
$diff floor($seconds 86400);
            break;
       case 
"h":    // hours
            
$diff floor($seconds 3600);
            break;
       case 
"i":    // minutes
            
$diff floor($seconds 60);
            break;
       case 
"s":    // seconds
            
$diff $seconds;
            break;
    }
    return 
$diff;