I have a car rent site where people can book a car for their needs. The problem is my vehicles are limited so after booking a car I want to immediately show an alert for the next 1 hour that it is overbooked so no one can book it again within that hour.

Here is my approach-

  • Fetching only today's booking info
  • Checking whether every vehicle is booked or not
  • If it is booked, then count the booking time and current time and compare them. Is it equal to or less then 1 hour?
  • If it returns true then show the alert

Now problem is how can I remove this message after 1 hour?

Any code example would be highly appreciated. Thanks

  • 1
    Hi. Thanks for your question, but... How is that connected with WordPress exactly? – Krzysiek Dróżdż May 5 at 6:51
  • How did you checked and show the message? – Vishwa May 5 at 9:11

Guys found the solution at **stackoverflow**:

You can use set_transient function after someone booked a car. I assume a car is post type and all post have a unique ID. So after a car is booked you create a transient which expires after one hour. Check Documentation. Your code might have the next logic.

set_transient( $post_id . '_car_is_booked', true, HOUR_IN_SECONDS );

You set transient for HOUR_IN_SECONDS it is a special WordPress variable and after one hour it will be removed from the database automatically. So to find our is this car is booked or not you can use function get_transient get_transient Docs.

// return false if option does not exists 
get_transient( $post_id . '_car_is_booked' );

And if transient for specific post ID exists it means the car is booked less then one hour ago.

Not the answer you're looking for? Browse other questions tagged or ask your own question.