1

深夜になると頭がおかしくなりますか?私は今、その夜の 1 つを過ごしており、これまでのところ私の関数は正常に機能していません。それを見てください: (私は PHP 5.2.9 を使用していることに注意してください。関数 /メソッドDateTime:Diff()は、PHP 5.3.0 まで使用できません。

<?php
    function time_diff($ts1, $ts2) {
        # Find The Bigger Number
        if ($ts1 == $ts2) {
            return '0 Seconds';
        } else if ($ts1 > $ts2) {
            $large = $ts1;
            $small = $ts2;
        } else {
            $small = $ts1;
            $large = $ts2;
        }
        # Get the Diffrence
        $diff = $large - $small;
        # Setup The Scope of Time
        $s = 1;         $ss = 0;
        $m = $s * 60;   $ms = 0;
        $h = $m * 60;   $hs = 0;
        $d = $h * 24;   $ds = 0;
        $n = $d * 31;   $ns = 0;
        $y = $n * 365;  $ys = 0;
        # Find the Scope
        while (($diff - $y) > 0) { $ys++; $diff -= $y; }
        while (($diff - $n) > 0) { $ms++; $diff -= $n; }
        while (($diff - $d) > 0) { $ds++; $diff -= $d; }
        while (($diff - $h) > 0) { $hs++; $diff -= $h; }
        while (($diff - $m) > 0) { $ms++; $diff -= $m; }
        while (($diff - $s) > 0) { $ss++; $diff -= $s; }
        # Print the Results
        return "$ys Years, $ns Months, $ds Days, $hs Hours, $ms Minutes & $ss Seconds.";
    }
    // Test the Function:
    ediff(strtotime('December 16, 1988'), time());
    # Output Should be:
    # 20 Years, 11 Months, 8 Days, X Hours, Y Minutes & Z Seconds.
?>
4

4 に答える 4

2

これはあなたの質問に対する答えではありませんが、私はただ指摘したかったのです...

while (($diff - $y) > 0) { $ys++; $diff -= $y; }

非常に非効率的な書き方です

$ys = $diff / $y;
$diff = $diff % $y;

また、これ

       else if ($ts1 > $ts2) {
                $large = $ts1;
                $small = $ts2;
        } else {
                $small = $ts1;
                $large = $ts2;
        }
        # Get the Diffrence
        $diff = $large - $small;

簡単に書き直すことができます

$diff = abs($ts1 - $ts2);

あなたのコードの問題は、それがより冗長でなければ、より明白になるだろうと私は感じています。:)

于 2009-11-25T00:02:44.040 に答える
2

シンプルで最初の部分を単純化するのはどうですか

$diff = abs($ts2 - $ts1);

次に、これを行うと:

 $n = $d * 31;   $ns = 0;
 $y = $n * 365;  $ys = 0;

あなたは実際に1年は36531日の長い月で構成されていると言っています。これは実際には約36年の長さです。おそらくあなたが望むものではありません。

最後に、私たちは皆ここで大人になっています。$ysの代わりに$YEAR_IN_SECONDSなどの成長した変数名を使用してください。はっきりとわかるように、コードを1回書くこともできますが、他の20のschmucksはそれを何度も読まなければなりません。

于 2009-11-25T00:03:34.217 に答える
2

指定されたタイムスタンプ中にすべての月が必要な場合は、php で次のコーディングを使用します。

function MonthsBetweenTimeStamp($t1, $t2) {
   $monthsYear = array();
   $lastYearMonth  = strtotime(gmdate('F-Y', $t2));
   $startYearMonth = strtotime(gmdate('F-Y', $t1));
    while ($startYearMonth < $lastYearMonth) {
        $monthsYear[] = gmdate("F-Y", $startYearMonth);
                    //Increment of one month directly 
        $startYearMonth = strtotime(gmdate("F-Y", $startYearMonth) . ' + 1 month');
    }

    if (empty($monthsYear)) {
        $monthsYear = array($startYearMonth));
    }

    return $monthsYear; 
于 2014-03-31T12:56:26.670 に答える
1

これはどう:

function time_diff($t1, $t2)
{
   $totalSeconds = abs($t1-$t2);
   $date = getdate($totalSeconds); 
   $firstYear = getdate(0);
   $years = $date['year']-$firstYear['year'];
   $months = $date['mon'];
   $days = $date['mday'];
   $hours = $date['hour'];
   $minutes = $date['minutes'];
   $seconds = $date['seconds'];

   return "$years Years, $months Months, $days Days, $hours Hours, $minutes Minutes & $seconds Seconds.";
}

これは、指定された時間の差を日付として使用します。次に、「getdate」にすべての作業を任せることができます。唯一の問題は年数です。これは単純に (差の) getdate 年から Unix エポック年 (1970 年) を引いたものです。

実際の月を使用したくない場合は、「年」の日を 12 の等しい月の日数で割ることもできます。

$months = $date['yday'] / (365/12);

同様に、モジュラスを使用して残りの日数を計算できます

$days = $date['yday'] % (365/12);
于 2009-11-25T01:01:46.997 に答える