I have a tabe that store expires timestamps
I want do something between 1 day before timestamps until 3 days after that
I try this:
$timeStamp = 1682936936; //2023-5-1 13:58 for example
$nowTime = strtotime(date("Y-m-d H:i:s"));
$oneDayBefore = strtotime("-1 day",$timeStamp);
$threeDaysAfter = strtotime("+3 days",$timeStamp);
if($nowTime > $oneDayBefore && $nowTime < $threeDaysAfter){
echo "yes";
}
else{
echo "no";
}
but return not correct results
2
Answers
this worked good:
i add default timezone and remove second from now time and add = to < or > now work and results is true for every timestamp
Specific problem with your code as-written:
strtotime()
takes a date string, but you’re giving it a timestamp. You need to feed it$nowTime
instead, eg:Also, if you use the DateTime functions instead you don’t have to convert back and forth between strings and rely on functions just magically knowing what your date format is:
Output:
DateTime object allow far more control than the old utility functions like
strtotime()
, especially if you need to do anything with timezones.Ref: https://www.php.net/manual/en/book.datetime.php