Skip to content
Advertisement

Php check if 24hours has passed in a specific timezone

I want to check if 24hour has passed from the time a token is generated. When I create a token I save the current timestamp and check if the timestamp is valid before making use of the token, I have tried below code but none worked, it keeps showing that token is valid even when the timestamp is more than 2 days.

JavaScript

And I tried this also

JavaScript

Advertisement

Answer

JavaScript

You have just a small logical error in your if – swap $currenTimestamp and $timestamp

$currentTimestamp is (normally) always bigger than your saved $timestamp, so if you substract the timestamp from the currentTimestamp, the result will be

“elapsed seconds since saved timestamp”

If this duration is bigger than 24 hours ==> expired token

Just as info: If you are only dealing with timestamps, the timezone isn’t really relevant. A timestamp is always independant from a timezone. If you want to check for 24 hours difference, just stick to timestamps. If you want to check for “1 day” it`s different, because the length of a day can be affected by e.g. a Winter/Summer-Time change

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement