Changes

Working with Dates and Times in MySQL

891 bytes added, 15:42, 23 October 2007
Inserting Date and Time Values into Table Columns
We will begin by adding a new order row. The order_no field will auto increment so we need to provide an order_item, an order date and a delivery date. The order date is the time that we place the order, so we can use the ''NOW()'' function to place the current date and time into the row.
The delivery date will be some period of time after the order dateso we can calculate this using the DATE_ADD() function which takes as arguments the start date (in our case NOW()) followed by the INTERVAL (in our case 14 days).For example: <pre>INSERT INTO orders (order_item, order_date, order_delivery) VALUES ('iPhone 8Gb', NOW(), DATE_ADD(NOW(), INTERVAL 14 DAY));</pre> The above statement will create an order for the specified item with the statement execution date and time as the order date and a date two weeks into the future as the delivery date: <pre>mysql> SELECT * FROM orders;+----------+------------+---------------------+----------------+| order_no | order_item | order_date | order_delivery |+----------+------------+---------------------+----------------+| 1 | iPhone 8Gb | 2007-10-23 11:37:55 | 2007-11-06 |+----------+------------+---------------------+----------------+1 row in set (0.00 sec)</pre>