2016-09-01 11 views
2

シーンはなので、夜は午前1時にオープンし、午後10時に閉じるストアがあります。 現時点では、タイムスタンプが店舗の開店時間と閉店時間の間にあるかどうかをチェックしたいだけです。phpの2つのタイムスタンプを比較する

これは非常に単純ですが、それでも私はなぜそれが難しいか分かりません。下の は私が試している壮大なたわごとです。

<?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)は条件が偽である理由1つのヘルプ、。 私は何か非常に一般的なものを見逃しているか、深刻な大失敗をしていますか?

ありがとうございます。

+0

私の悪いところです。$ o&$ cを文字列に変換するのを忘れて、日付を文字列と比較していました。 – Sarge

答えて

2

これを試してみてください代わりに

$o$cが文字列であり、$ tは()の時間であるため、だ
$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

。 あなたはそれが正しく動作するために

if ($t < strtotime($c)) 

if ($t > stttotime($o)) 

にあなたのIFSを変更する必要があります。

+0

イェップ私はそれを得た、それは私の側からの大失敗です。バディに感謝します。 – Sarge

関連する問題