2009-07-02 20 views
5

私はDateオブジェクト(Pearから)を持っていて、別のDateオブジェクトを減算して時差を秒で求めたいと思っています。PHPの日付/時刻の差を計算する

私はいくつかのことを試しましたが、最初はちょうど私に日の違いを与えてくれました。そして2回目は固定の時刻をUnixタイムスタンプに変換できますが、Dateオブジェクトには変換できません。

 $now = new Date(); 
     $tzone = new Date_TimeZone($timezone); 
     $now->convertTZ($tzone); 
     $start = strtotime($now); 
     $eob = strtotime("2009/07/02 17:00"); // Always today at 17:00 

     $timediff = $eob - $start; 

**注**常に24時間未満の差異があります。

+0

はあなたのstrtotime(に供給文字列と同じになりました$の出力形式です):それは...ここに私のコードであるなど、「2日前」「1分前」を示していますか?つまり、 "yyyy/mm/dd H:i" – Mathew

+1

おそらくそれは私だけですが、PHPドキュメントの "Date"クラスの説明を見つけることができないようです。それは何のライブラリですか? –

+0

なぜあなたはそれらのうちの1つだけを別のタイムゾーンに変換しますか?ターゲットTZの両方で現地時間または両方を使用してはいけませんか? –

答えて

1

はまだ多少間違った値を与えたが、私は周りのPEAR日の古いバージョンを持って考えると、多分それはあなたのために働くか、あなたに:)

を修正する方法についてのヒントを提供します
<pre> 
<?php 
    require "Date.php"; 

    $now = new Date(); 
    $target = new Date("2009-07-02 15:00:00"); 

    //Bring target to current timezone to compare. (From Hawaii to GMT) 
    $target->setTZByID("US/Hawaii"); 
    $target->convertTZByID("America/Sao_Paulo"); 

    $diff = new Date_Span($target,$now); 

    echo "Now (localtime): {$now->format("%Y-%m-%d %H:%M:%S")} \n\n"; 
    echo "Target (localtime): {$target->format("%Y-%m-%d %H:%M:%S")} \n\n"; 
    echo $diff->format("Diff: %g seconds => %C"); 
?> 
</pre> 
0

Pear Dateオブジェクトの変換 - >文字列 - >タイムスタンプは確実に機能しますか?あなたはdocumentation

$start = $now->getTime(); 
0

に応じて17までの秒」を見つけるために、梨なしでそれを行うには、このようなタイムスタンプを取得することができ

$start = strtotime($now); 

代わりに:それはここで行われているものです。 00にすることができます:

$current_time = mktime(); 
$target_time = strtotime (date ('Y-m-d'. ' 17:00:00')); 
$timediff = $target_time - $current_time; 

これはテストされていませんが、必要な処理を行う必要があります。

0

Dateオブジェクト全体をstrtotimeに渡す必要はありません。代わりにこれらのいずれかを使用してください。

$start = strtotime($now->getDate()); 

または

$start = $now->getTime(); 
0

多分一部の人々は、時間差をFacebookの方法で持っていたいと思うかもしれません。

function getTimeDifferenceToNowString($timeToCompare) { 

     // get current time 
     $currentTime = new Date(); 
     $currentTimeInSeconds = strtotime($currentTime); 
     $timeToCompareInSeconds = strtotime($timeToCompare); 

     // get delta between $time and $currentTime 
     $delta = $currentTimeInSeconds - $timeToCompareInSeconds; 

     // if delta is more than 7 days print the date 
     if ($delta > 60 * 60 * 24 *7) { 
      return $timeToCompare; 
     } 

     // if delta is more than 24 hours print in days 
     else if ($delta > 60 * 60 *24) { 
      $days = $delta/(60*60 *24); 
      return $days . " days ago"; 
     } 

     // if delta is more than 60 minutes, print in hours 
     else if ($delta > 60 * 60){ 
      $hours = $delta/(60*60); 
      return $hours . " hours ago"; 
     } 

     // if delta is more than 60 seconds print in minutes 
     else if ($delta > 60) { 
      $minutes = $delta/60; 
      return $minutes . " minutes ago"; 
     } 

     // actually for now: if it is less or equal to 60 seconds, just say it is a minute 
     return "one minute ago"; 

    }