2010-06-12 5 views
3

ici est mon codedatetime diff ne fonctionne pas

function check($dt) { 
    $date = date("Y-m-d"); 
    $start = new DateTime($date); 
    $end = new DateTime($dt); 
    $diff = $start->diff($end); 

    return $diff->format('%d days'); 
    } 

print check('2009-12-14'); 

qui imprime 29 jours

Où suis-je tort?

Répondre

6

Il est expliqué dans le manual:

<?php 

$january = new DateTime('2010-01-01'); 
$february = new DateTime('2010-02-01'); 
$interval = $february->diff($january); 

// %a will output the total number of days. 
echo $interval->format('%a total days')."\n"; 

// While %d will only output the number of days not already covered by the 
// month. 
echo $interval->format('%m month, %d days'); 

?> 

Vous voulez:

function check($dt) { 
    $date = date("Y-m-d"); 
    $start = new DateTime($date); 
    $end = new DateTime($dt); 
    $diff = $start->diff($end); 

    return $diff->format('%a days'); 
} 

print check('2009-12-14'); 

donne 180 days.

+0

oh oui bien, j'ai raté cette partie dans le manuel –