2010-08-04 70 views
15

我有兩次存儲在變量$resttimefrom$resttimeto中的格式,分別爲7:30:00和22:30:00。如何在PHP中比較兩次

我想檢查當前時間是否在這兩個值之間。我正在用代碼檢查這個

$time = date("G:i:s"); 
if ($time > $resttimefrom and $time < $resttimeto){ 
    $stat = "open"; 
} else { 
    $stat = "close"; 
} 

但我總是得到$ stat作爲關閉。什麼可能導致?

+0

由於其簡單的邏輯! – Rajasekar 2010-08-04 10:10:37

+0

可能重複的[PHP的比較日期](http://stackoverflow.com/questions/2113940/php-compare-date) – Kuf 2013-01-24 17:00:25

+0

這是因爲$ resttimefrom和$ resttimeto沒有定義 – Black 2015-11-05 09:57:22

回答

-3

正如上校彈片還說我通過轉換到秒,所有的時間,然後做它當前時間的總秒數與比較

+0

你的意思是,這是被接受的答案? – 2017-10-31 09:20:32

3

您正在比較字符串。
使用strtotime()將時間字符串轉換爲時間戳。
然後與time()比較。

3
$today = date("m-d-y "); 
$now = date("m-d-y G:i:s"); 

if (strtotime($today . $resttimefrom) < $now && $now > strtotime($today . $resttimeto)) { 
    $stat = 'open'; 
else 
    $stat = 'close 
2

date函數返回一個字符串,那麼你正在做將是一個字符串比較的比較 - 所以7:30將超過22:30

這將是更好的使用mktime ,它會返回一個Unix時間戳值(整數),所以它將使一個更好的比較

$currentTime = mktime(); 
$resttimefrom = mktime(hour,min,second); 

http://php.net/mktime

+0

正如上面的答案中所述:if時間以字符串形式提供給您,那麼使用strtotime會更容易 – 2010-08-04 08:43:39

+0

當不帶參數調用時,'mktime()'拋出一個'E_STRICT'通知。此外,當DST是一個因素時,您的代碼可能會導致錯誤的結果。 'mktime'有一個標誌來指示DST,但是從PHP5.3開始,如果使用了'is_dst'參數,它也會拋出'E_DEPRECATED'通知。 – Gordon 2010-08-04 08:55:49

4

嘗試將它們重新格式化爲可以比較的東西。例如,數字:

$resttimefrom = mktime(7,30,0); 
$resttimeto = mktime(22,30,0); 

$time = mktime(date('H'),date('i'),date('s')); 
+0

不考慮夏令時。 – Gordon 2010-08-04 08:56:23

1

只需將您的日期轉換爲Unix時間戳,比較它們即可獲得結果!它可能是這個樣子:

$time =date("G:i:s"); 

$time1 = strtotime($time); 
$resttimefrom1 = strtotime($resttimefrom); 
$resttimeto1 = strtotime($resttimeto); 
if ($time1 >$resttimefrom and $time1 <$resttimeto) 
{ 
    $stat="open"; 
} 
else 
{ 
    $stat="close"; 
} 
0
$firstTime = '1:07'; 
$secondTime = '3:01'; 

list($firstMinutes, $firstSeconds) = explode(':', $firstTime); 
list($secondMinutes, $secondSeconds) = explode(':', $secondTime); 

$firstSeconds += ($firstMinutes * 60); 
$secondSeconds += ($secondMinutes * 60); 
$difference = $secondSeconds - $firstSeconds; 
20

一個簡單而聰明的方式做,這是去除「:」從你的日期。

$resttimefrom = 73000; 
$resttimeto = 223000; 

$currentTime = (int) date('Gis'); 

if ($currentTime > $resttimefrom && $currentTime < $resttimeto) 
{ 
    $stat="open"; 
} 
else 
{ 
    $stat="close"; 
} 
+0

好主意。我試圖在沒有'strtotime'的Twig中做到這一點,這很好地工作。 – regularmike 2015-11-09 04:32:16

1

訣竅操縱和在PHP比較日期和時間是存儲的日期/時間值的整數變量,並使用mktime(),日期()和的strtotime()函數。日期/時間的整數表示是自1970年1月1日午夜以來的秒數,其被稱爲「時代」。一旦你的日期/時間是整數形式,你就可以有效地將它與其他也是整數形式的日期進行比較。

當然,因爲你很可能會被從頁面的請求和數據庫接收的日期/時間值中選擇你需要的日期/時間字符串轉換爲整數,你可以做任何比較或算術之前查詢。

假設您確定$ resttimefrom和$ resttimeto變量包含格式正確的時間,您可以使用strtotime()函數將字符串時間轉換爲整數。 strtotime()接受格式化爲日期的字符串,並將其轉換爲自epoch以來的秒數。

$time_from = strtotime($resttimefrom); 
$time_to = strtotime($resttimeto); 

附註:strtotime()總是返回整數形式的完整日期。如果你的字符串沒有日期,只有一次,strtotime()返回今天的日期以及你在字符串中給出的時間。然而,這對你並不重要,因爲strtotime()返回的兩個日期將具有相同的日期,並且比較這兩個變量將具有期望的效果,即在日期相互抵消時比較兩次。

當您比較兩個整數時請記住,日期/時間越早,其整數值越小。所以,如果你想看看是否$ time_from低於$ time_to早,你有這樣的:

if ($time_from < $time_to) 
{ 
    // $time_from is ealier than $time_to 
} 

我們的日期/時間與當前系統日期/時間比較,只需使用mktime()不帶參數表示當前日期/時間:

if ($time_from < mktime()) 
{ 
    // $time_from is in the past 
} 
+0

p.s.不要使用沒有參數的mktime()。雖然這仍然有效,但現在已被棄用。使用time()來獲得自epoch以來的秒數。 – emurano 2010-10-25 12:28:19

21

你可以嘗試使用strtotime

$st_time = strtotime($resttimefrom); 
$end_time = strtotime($resttimeto); 
$cur_time = strtotime(now); 

然後檢查

if($st_time < $cur_time && $end_time > $cur_time) 
{ 
    echo "WE ARE CLOSE NOW !!"; 
} 
else{ 
    echo "WE ARE OPEN NOW !!"; 
} 

我希望這可以幫助你..

+1

+1幫助,但爲了避免錯誤「注意:現在使用未定義的常量 - 假定'now'」,將關鍵字封裝到引號中 – MrSo 2016-08-05 08:20:34

0
$Time1 = date_parse($time); 
$seconds1 = $Time1['hour'] * 3600 + $Time1['minute'] * 60 + $Time1['second']; 

$Time2 = date_parse($current_time); 
$seconds2 = Time2['hour'] * 3600 + Time2['minute'] * 60 + Time2['second']; 

$actula_time = $seconds1 - $seconds2; 

echo floor($actula_time/3600) .":". floor(($actula_time/60)%60) .":". $actula_time%60;