PHP的DATE運算

假若我已經將此資料寫入MYSQL裡面了

$date=date("Y-m-d H-i-s");

我想要將資料讀出 並且加以計算...

我想要將$date_1=$date加1個月的時間該如何做?

我想要計算$date_1-$date=剩餘幾天幾小時幾分鐘如何做

Update:

$seconds = ( $date_1 - $date )

$secs=strtotime($date1)-strtotime($date);

兩位大大的我都試過了...可是得知結果應該要等於30天,可是計算不出來

2 Answers

Rating
  • 志國
    Lv 7
    1 decade ago
    Favorite Answer

    $date_1 = date("Y-m-d", strtotime( date("Y-m-d", $row["mydate"] ) ) + 86400 * 30 );

    86400 秒等於一天,我都會這樣寫 86400 * 30,利於程式碼閱讀,若您直接拿計算機算好輸入,以後容易忘 (加註解亦可)

    $seconds = ( $date_1 - $date ) / 60;

    $minutes = $seconds / 60;

    以此類推,懂了吧^_^

    2007-11-26 23:37:01 補充:

    我舉例為天數,您要精確當然都用

    date("Y-m-d H-i-s") 取到秒

    2007-11-27 14:03:11 補充:

    哈我打太快了..

    $minutes = ( $date_1 - $date ) / 60;

    $hours = $minutes / 60;

    2007-11-27 14:03:45 補充:

    評論者: Peerage 所述非常有見解,推~

  • ?
    Lv 5
    1 decade ago

    $datedate("Y-m-d H-i-s");

    echo $date1=date("Y-m-d H:i:s",strtotime("+1 month",strtotime($date)));

    $secs=strtotime($date1)-strtotime($date);

    echo floor($secs/86400)."天".floor(($secs%86400)/3600)."時".floor(($secs%3600)/60)."分".($secs%60)."秒";

Still have questions? Get your answers by asking now.