I have a database with some tables from a restaurant and users can see if they are free of reserved. If free they cand book that table.

Now, my question is this: What can i do so that after (let's say 1 day (or any amount of time) after the booking was made to auto change tofree.

Dani AI

Generated

For : the cleanest model is to record an expiry time when a table is booked and let logic decide a table is available when that expiry has passed. That avoids fragile state transitions and makes detection trivial. is right — a timed job is common — and ’s page-load idea can work as a fallback if you lack scheduling access. Below are practical options, sample queries, and cautions.

Store an expires_at (DATETIME or TIMESTAMP in UTC) when creating a booking. To treat expired reservations as available without changing rows immediately, use a query like this when showing free tables:

SELECT * FROM restaurant_tables
WHERE status = 'free'
   OR (status = 'reserved' AND expires_at <= NOW());

If you want rows updated periodically (so reports show correct status), use one of these scheduled approaches:

  • MySQL Event Scheduler (runs inside the DB). Example event:
CREATE EVENT IF NOT EXISTS release_old_bookings
ON SCHEDULE EVERY 1 HOUR
DO
  UPDATE restaurant_tables
  SET status = 'free'
  WHERE status = 'reserved' AND expires_at < NOW();

See MySQL events documentation for details and privileges: MySQL Event Scheduler.

  • System cron (or external webcron) calling a small PHP script that runs the same UPDATE. Example crontab line:
0 * * * * /usr/bin/php /var/www/cron/release_old_bookings.php

Notes and cautions: ensure timestamps are stored/compared in a consistent timezone (store UTC; convert for display) — see PHP timezones. Use atomic UPDATE WHERE clauses to avoid race conditions (check status in the WHERE). On shared hosting where cron or EVENT is unavailable, use the page-load lazy-cleanup but limit frequency (store last-run timestamp in DB) to avoid per-request work. Finally, test on a copy of the DB before running automated updates.

Recommended Answers

All 2 Replies

I'm not sure exactly what you are trying to accomplish but if you need to run a program on a timed basis, then you need to have a look at Cron. It's usually available on most servers.

If you can't do Cron, you can also have a small script that runs on each page load and checks the time vs. the time it was last run (probably keep in a database table). If the time is more than 1 day after the last time it was run, then include the script you want to run.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.