2011-07-01 13 views
1

2つの日付 - $end_date$start_dateがある場合、「2年:4ヶ月:2日"?2つの日付の差を人間が判読可能な形式で表現する方法

私はそうのような2つの日付の差を得ることができることを知っている:それは上図のように

$dif=strtotime($end_date)-strtotime($today); 

しかし、どのように私は、人間が読める形式にその結果を変換することができますか?

+0

希望する出力の例を含めることはできますか? –

+0

@Tim Cooper、 これは単純な結果です - 89337600 – Learner

+0

@Ceylo:私は実際にフォーマットされたタイムスタンプを意味します。 –

答えて

2

これは、あなたがタイムスタンプをフォーマットすることができる方法である。

echo date('d-m-Y', strtotime($end_date)) // for DD-MM-YYYY format 

あなたは数日以内に、2つの日付の差を計算するために探していますか?

EDIT:「XXyear YYmonth ZZday」の日付の違いを見つけるコード。このコードでは、開始日と終了日はYYYY-MM-DD形式であると想定しています。そうでない場合は、YYYY-MM-DD形式に変更するか、それに応じて引数をmktime()に変更してください。

$endDate = '2011-03-01'; 
$startDate = '2011-02-02'; 
$daysPerYear = 365; 
$daysPerMonth = 30.4; 
$diffDay = $diffMonth = $diffYear = 0; 

$endDateTs = mktime(0, 0, 0, substr($endDate, 5, 2), substr($endDate, 8, 2), substr($endDate, 0, 4)); 
$startDateTs = mktime(0, 0, 0, substr($startDate, 5, 2), substr($startDate, 8, 2), substr($startDate, 0, 4)); 
$diffDay = ($endDateTs - $startDateTs)/60/60/ 24; // difference between 2 dates in number of days 
$diffYear = floor($diffDay/$daysPerYear); // difference in years 
$diffDay = $diffDay % $daysPerYear; // balance days 
$diffMonth = floor($diffDay/$daysPerMonth); // difference in months 
$diffDay = ceil($diffDay % $daysPerMonth); // balance days 
echo ($diffYear ? $diffYear . 'year ' : '') . ($diffMonth ? $diffMonth . 'month ' : '') . ($diffDay ? $diffDay . 'day' : ''); 

注:私は、必要に応じて微調整すること自由に感じなさいなどうるう年を含むすべての可能な日付の組み合わせ、のコードをテストしていません。

これが役に立ちます。

+1

はいそれは私が必要なものです.Y:M:D.の形式に – Learner

+0

@Ceylo:私の編集を見てください;それが役に立てば幸い – Abhay

0
function is_leap_year($year) 
{ 
    if($year % 4 == 0) 
    { 
     if($year % 100 == 0) 
     { 
      if($year % 400 == 0) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
     else 
     { 
      return true; 
     } 
    } 
    else 
    { 
     return false; 
    } 
} 

function calculate_date($now, $end){ 
    $years = date('Y', strtotime($end)) - date('Y', strtotime($now)) ; 
    if($years < 0) 
    { 
     return "Error: year"; 
    } 
    $mounths = date('m', strtotime($end)) - date('m', strtotime($now)) ; 
    if($mounths < 0) 
    { 
     if($years < 1) 
     { 
      return "Error: mounth and year"; 
     } 
     else 
     { 
      $years --; 
      $mounths += 12; 
     } 
    } 
    $days = date('d', strtotime($end)) - date('d', strtotime($now)) ; 
    if($days < 0){ 
     if($mounths < 1) 
     { 
      if($years < 1) 
      { 
       return "Error: day, mounth and year"; 
      } 
      else 
      { 
       $years --; 
       $mounths += 12; 
      } 
     } 
     else 
     { 
      $mounths --; 
      switch (date('m', strtotime($now))) 
      { 
       case 1: 
       case 3: 
       case 5: 
       case 7: 
       case 8: 
       case 10: 
       case 12: 
        $days +=31; 
        break; 
       case 4: 
       case 6: 
       case 9: 
       case 11: 
        $days +=30; 
        break; 
       case 2: 
        if(is_leap_year(date('Y', strtotime($now)))) 
        { 
         $days += 29; 
         break; 
        } 
        else 
        { 
         $days += 28; 
         break; 
        } 
      } 
     } 
    } 
    return $years . " Years : " . $mounths . " Months : " . $days . " Days Remaining."; 
} 

$end_date = new DateTime('2011-08-05'); 
$end_date = $end_date->format('d-m-Y'); 
$today = date('d-m-Y'); 

$remaining = calculate_date($today, $end_date); 

echo $remaining; 

そして、あなたは、あなたが使用することができますEND_DATEフォーマットしたい場合: 日付( 'D-M-Y'、のstrtotime($ END_DATE)) 、その後、あなたはcalculate_dateで残り時間を計算することができます。

0

粗い違いがあれば(「2年前」)、Date_HumanDiff PEARパッケージを試してみるとよいでしょう。

関連する問題