2016-09-01 28 views
2

中的兩個時間戳所以現場是,我有一家店在晚上1點開門,晚上10點關門。 對於任何當前時間,我只是想檢查時間戳是否位於商店開放時間和關閉時間之間。比較php

Yeap很簡單,但我不知道爲什麼,我覺得很難。下面的 是我嘗試的一段史詩般的狗屎。

<?php 

$t=time(); //for current time 
$o = date("Y-m-d ",$t)."01:00:00"; //store open at this time 
$c = date("Y-m-d ",$t)."22:00:00"; //store closes at this time 

//Yes, its crazy .. but I love echoing variables 
echo "current time = ".$t; 
echo PHP_EOL; 
echo "Start = ".strtotime($o); 
echo PHP_EOL; 
echo "End = ".strtotime($c); 
echo PHP_EOL; 

// below condition runs well, $t is always greater than $o 
if($t>$o){ 
    echo "Open_c1"; 
}else{ 
    echo "Close_c1"; 
} 

//Here's where my variable $t, behaves like a little terrorist and proclaims itself greater than $c 
if($t<$c){ 
    echo "Open_c2"; 
}else{ 
    echo "Close_c2"; 
} 
?> 

OUTPUT:上phpfiddle

當前時間= 1472765602開始= 1472706000結束= 1472781600 Open_c1 Close_c2

只是一個幫助,爲什麼($ T < $ C)條件爲假。 我錯過了一些很常見的東西,或者犯了一個嚴重的錯誤。

謝謝。

+0

我的壞..我忘了將$ o&$ c轉換爲字符串,並將字符串與日期進行比較。 – Sarge

回答

2

試試這個

$o = date("Y-m-d 01:00:00"); //store open at this time 
$c = date("Y-m-d 22:00:00"); //store closes at this time 
+0

謝謝你,我的壞..我忘了把它轉換成字符串,並與字符串進行比較。 – Sarge

2

這是因爲$o$c是字符串,$ t爲時間(); 你需要改變你的IFS到

if ($t < strtotime($c)) 

if ($t > stttotime($o)) 

它才能正常工作。

+0

是的,我明白了,這是我身邊的一個錯誤。感謝哥們。 – Sarge