簡體   English   中英

比較php中的兩個時間戳

[英]Comparing two timestamps in php

所以場景是,我有一家商店,晚上 1:00 開門,晚上 10:00 關門。 對於任何當前時間,我只想檢查該時間戳是否位於商店開放時間和關閉時間之間。

是的,這很簡單,但我仍然不知道為什么,我覺得很難。 下面是我正在嘗試的一段史詩般的東西。

<?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";
}
?>

輸出:在 phpfiddle

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

只是一個幫助,為什么 ( $t < $c ) 條件為假。 我是否遺漏了一些非常常見的東西或犯了嚴重的錯誤。

謝謝。

那是因為$o$c是字符串,而 $t 是 time(); 您需要將您的 ifs 更改為

 if ($t < strtotime($c))

 if ($t > strtotime($o))

使其正常工作。

試試這個

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM