Search code examples
mysqldatabasedatabase-designsqldatatypes

Which MySQL data type to use for scheduling?


I want to record the start and end times of, say, movies playing at the cinemas. I need to store the date (e.g. Jan 1, 2011), start time (e.g. 9:00am), and end time (e.g. 11am). Should I used DateTime? I've seen some implementations wherein an INT is used to represent time in minutes (e.g. 0 = 12 midnight, 1 = 12:01am, etc.). I'm not sure if there is some sort of benefit to using INT over DateTime.

Suggestions?

EDIT 1:

Expanding on the idea of using INT as described above, the table would probably have the following columns:

  • date (e.g. Jan 1, 2011)
  • start_time (e.g. 540 - to indicate 9am)
  • end_time (e.g. 660 - to indicate 11am)

Solution

  • Use DATETIME or INT

    I prefer to store date&time in unix timestamp (INT), because i can use it in PHP functions like date

    echo "Movie will start at ".date("H:i", $row['start_time']);
    

    And it's easy to manipulate with it:

    echo "Movie will start in ".(time() - $row['start_time'])." seconds";
    

    Current date and time: http://www.unixtimestamp.com/

    If you want to copy movies to next day:

     $sql = "SELECT * FROM movies WHERE start_time >= ".strtotime("today 00:00")." AND start_time <= ".strtotime("today 23:59");
    
     // query..
     foreach($results as $row) {
        $new_start = strtotime("+1 day", $row['start_time'];    
        // insert
     }