2017-12-15 4 views
3

私はこれをDATETIMEタイプのデータベースに保存しました。
はそれの価値を検討:2017年12月14日16時17分21秒データベースのフィールドからの残り時間を確認する

をそして、これは私のプログラムのロジックです。私は72時間が経過したかどうか確認したい他のエコーあなたは例えば48時間22分55秒秒を待つ必要があります。

$row = $res->fetch_assoc(); 
$last_dl = $row['last_dl']; 

// To seconds from 1970 
$last_dl = strtotime($last_dl); //120 
$seventy_two_hours_in_seconds = 60 * 60 * 72; 

if (time() > ($last_dl + $seventy_two_hours_in_seconds)) { 
    echo "72 Passed"; 
} else { 
    echo "You need to wait: " . date('H/h i/m s/s',mktime(0, 0, ($last_dl + $seventy_two_hours_in_seconds) - time())); 
} 

ロジックに問題があります。時間が少ないことが分かります。

+0

[ 'DATE_ADD()'](HTTPSを使用して:// WWWを。 w3schools.com/sql/func_mysql_date_add.asp)は、MySQLを使用している場合に役に立ちます。 – Swellar

+0

date_default_timezone_set() –

+0

@MrKen Nope。それはうまくいかなかった。 –

答えて

1

あなたが待つために、第2の時間を計算し、時間、分、秒でそれ存在した後ことができます。

<?php 
$row = $res->fetch_assoc(); 
$last_dl = $row['last_dl']; 

// To seconds from 1970 
$last_dl = strtotime($last_dl); //120 
$seventy_two_hours_in_seconds = 60 * 60 * 72; 

if (time() > ($last_dl + $seventy_two_hours_in_seconds)) { 
    echo "72 Passed"; 
} else { 
    $wait = date('U', $last_dl + $seventy_two_hours_in_seconds) - date('U'); // seconds for wait 
    $hours = floor($wait /3600); 
    $min = floor(($wait/60) % 60); 
    $sec = $wait % 60; 
    echo "You need to wait: ". $hours. ':'. $min.':'.$sec ."\n"; 
} 

http://sandbox.onlinephpfunctions.com/code/08477cd78c82dfe2b4a5c5f4aee6414866999a8c

関連する問題