指定された時刻 (たとえば 23:55) があり、現在の時刻が 23:55 から 00:10 の間である場合に続行したい場合、どうすればよいですか? 指定された時間には日付が添付されていません。
$specifiedTime = '23:55';
$currentTime = '00:05';
$timeDifference = strtotime($currentTime) - strtotime($specifiedTime);
if ($timeDifference < 900) {
// proceed if less than 15 minutes has elapsed since specifiedTime
}
timeDifference は 86700 として返されます。2 つの時間の間の秒数である 600 を返すようにします。
更新: すべてに感謝します。これが作業コードです - これが基づいているコードを削除したユーザーに感謝します:
$currentTime = time();
$fromTime = strtotime('23:55');
$toTime = strtotime('+15 minute', $fromTime);
if ($fromTime <= $currentTime && $currentTime <= $toTime) {
echo '<br>Current time is within 15 minutes after from time';
} else {
echo '<br>Current time is not within 15 minutes after from time';
}
更新 2 :Galvic が指摘したように、これは特定の時間の間は機能しないため、Sumurai8 のソリューションを選択しました。