If you are going to have a column for the start time and one for the duration, I think you can save it in seconds. So, I assume that you will have something like this:
+-----------+--------------------------+------------------+ | advert_id | start_time | duration_seconds | +-----------+--------------------------+------------------+ | 2342342 |'2012-11-12 10:23:03' | 86400 | +-----------+--------------------------+------------------+
(For example, we will call this table adverts )
- advert_id is the key pointing to your ad.
- start_time - start time of the ad (data type - TIMESTAMP)
duration_seconds - The time, in seconds, during which the ad should "live" (INTEGER (11)
SELECT TIME_TO_SEC (timediff (now (), start_time)) as 'time_difference_in_seconds_since_advert_started' FROM adverts ;
If you want to receive only those ads that have not expired, you will run this request:
SELECT * FROM `adverts` WHERE TIME_TO_SEC(timediff(now(),start_time))<=`duration_seconds`;
This is one way to do this if I went with a duration field.
source share