Comparing two timestamps in php
10,529
Solution 1
That's because $o
and $c
are string and $t is time();
You need to change your ifs to
if ($t < strtotime($c))
and
if ($t > strtotime($o))
for it to work properly.
Solution 2
Try this instead
$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
Author by
Sarge
Updated on June 04, 2022Comments
-
Sarge almost 2 years
SO the scene is, I have a store which opens at 1:00am at night, and closes at 10:00pm. For any current time i just want to check whether that timestamp lies between store open and close times.
Yeap that's very simple, and still I don't know why, am finding it difficult. below is a piece of epic shit i am trying.
<?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: on phpfiddle
current time = 1472765602 Start = 1472706000 End = 1472781600 Open_c1 Close_c2
Just one help, why ( $t < $c ) condition is false. Am I missing something very common or making a serious blunder.
Thank you.