I am creating time stamps in PHP using time()
;
I have the $current_time
and $purchase_time
. How do I make sure that purchase_time is less than 24 hours of current time?
The difference is calculated by subtracting the second operand from the first. The result is rounded down, with any remainder discarded. For example, 61 minutes is equal to 1 hour, and 59 minutes is equal to 0 hours. The value returned by the function is compatible with both type numeric and type duration.
If you'd like to calculate the difference between the timestamps in seconds, multiply the decimal difference in days by the number of seconds in a day, which equals 24 * 60 * 60 = 86400 , or the product of the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.
First, calculate convert the dates to be compared into unix timestamps: date1=$(date --date="2020-10-31 05:41:33" "+%s") date2=$(date --date="2020-11-01 05:41:33" "+%s")
Method 1: Subtracting Unix timestampsYou can easily convert any date, in any format to a Unix timestamp using the in-built PHP function strtotime(). Now that you have the time difference in seconds, you can easily convert it to other durations of your preference such as hours, days, months, years, etc.
If they are UNIX timestamps, then you can calculate this by yourself really easy, as they are seconds.
$seconds = $current_time - $purchase_time
$hours = floor($seconds/3600);
if ($hours < 24){
//success
}
Since UNIX timestamps are just numbers of seconds, just use the difference:
$purchasedToday = $current_time - $purchase_time < 24 * 60 * 60;
if ($purchasedToday) {
echo 'You just bought the item';
} else {
echo 'You bought the item some time ago';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With