Frage

Wenn ich zwei Variablen $startDate="YYYYmmdd" und $endDate="YYYYmmdd", wie kann ich die Anzahl der Tage zwischen ihnen setzen Sie sich bitte?

Danke.

War es hilfreich?

Lösung

Wenn Sie PHP 5.3 verwenden, können Sie die neue verwenden DateTime Klasse:

$startDate = new DateTime("20101013");
$endDate = new DateTime("20101225");

$interval = $startDate->diff($endDate);

echo $interval->days . " until Christmas"; // echos 73 days until Christmas

Wenn nicht, müssen Sie verwenden strtotime :

$startDate = strtotime("20101013");
$endDate = strtotime("20101225");

$interval = $endDate - $startDate;
$days = floor($interval / (60 * 60 * 24));

echo $days . " until Christmas"; // echos 73 days until Christmas

Andere Tipps

<?php
function days($date1, $date2) {
    $date1 = strtotime($date1);
    $date2 = strtotime($date2);
    return ($date2 - $date1) / (24 * 60 * 60);
}
$date1 = '20100820';
$date2 = '20100930';
echo days($date1, $date2);
?>
$DayDiff = strtotime("2010-01-12")-strtotime("2009-12-30");
echo  date('z', $DayDiff)." Days";

Dies sollte man mit PHP präziser und nutzbar sein <5.2

<?php   
 $time1=strtotime($startDate);
    $time2=strtotime($endDate);
    $daycount=floor(($time2-$time1)/ 86400);
?>

Hier ist der Beispielcode

$startDate = mktime(0,0,0,1,1,2010); 
$endDate = mktime(0,0,0,12,1,2010); 

$dateDiff = $date1 - $date2;
$fullDays = floor($dateDiff/(60*60*24));
echo "Differernce is $fullDays days"; 

Der einfachste Weg, den ich gefunden habe, die Anzahl der Tage zwischen ihnen zu erhalten ist, indem Sie die Start- und Enddaten zu Unix Umwandlung Zeitstempel und auf sie eine subtrahieren tun.

Dann, wenn Sie das Datum konvertieren formatieren sie mit der PHP-Funktion date-Funktion zurück.

Hier ist mein Ansatz, basierend auf einer brutale Suche in den meisten Fällen nur weil Divisionen von Sekunden (für Wochen, Monate, Jahre) präzise Ergebnisse nicht zurückgeben können, wie während mit Schaltjahren arbeiten zum Beispiel.

<?php
function datediff( $timeformat, $startdate, $enddate )
{
    $unix_startdate = strtotime( $startdate ) ;
    $unix_enddate = strtotime( $enddate ) ;
    $min_date = min($unix_startdate, $unix_enddate);
    $max_date = max($unix_startdate, $unix_enddate);
    $Sd = date( "d", $unix_startdate ) ;
    $Sm = date( "m", $unix_startdate ) ;
    $Sy = date( "Y", $unix_startdate ) ;
    $Ed = date( "d", $unix_enddate ) ;
    $Em = date( "m", $unix_enddate ) ;
    $Ey = date( "Y", $unix_enddate ) ;

    $unixtimediff = $unix_enddate - $unix_startdate ;
    if ( $unixtimediff <= 0 ) return -1 ;

    switch( strtolower( $timeformat ) )
    {
         case "d": // days
         $divisor = 3600 * 24 ;
         return floor( $unixtimediff / $divisor ) + 1 ; 
         break ;
         case "w": // weeks
         $i = 0 ;
         while ( ( $min_date = strtotime("+1 DAY", $min_date) ) <= $max_date) $i++;
         return floor( $i / 7 ) ;
         break ;
         case "m": // months
         $i = $Sd != $Ed && $Sm != $Em ? 1 : 0 ;
         while ( ( $min_date = strtotime("+1 MONTH", $min_date) ) <= $max_date) $i++;
         return $i ;
         break ;
         case "q": // quaterly (3 months)
         $i = $Sd != $Ed && $Sm != $Em ? 1 : 0 ;
         while ( ( $min_date = strtotime("+3 MONTH", $min_date) ) <= $max_date) $i++;
         return $i ;
         break ;
         case "y": // year
         $i = $Sd != $Ed && $Sm != $Em ? 1 : 0 ;
         while ( ( $min_date = strtotime("+1 MONTH", $min_date) ) <= $max_date) $i++;
         return floor( $i / 12 ) ;
         break ;
    }
}

$startdate = "2014-01-01" ;
$enddate = "2015-12-31" ;
$formats = array( "d" => "days", "w" => "weeks", "m" => "months", "q" => "quaterly", "y" => "years" ) ;
foreach( $formats AS $K => $F )
echo "From $startdate to $enddate in $F format: ". datediff( "$K",  $startdate, $enddate )."<br>" ;

?>

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top