Database Reference
In-Depth Information
Working with Dates and Times 11
Using Date and Time
Putting a date and time column in a database is a simple task. Populating a row with the
current date and time is even easier in many scripting languages. However, getting the date
and time back, although sometimes confusing, can be an extremely powerful way of
analysing your data, and getting more from your tables.
NOW
To obtain the current date and time, use the NOW() function. The function requires the
open and close brackets even though you are not passing any value to it. To quickly obtain
the current date and time you can use a SELECT as follows:
SELECT NOW()
This will produce the output shown in Figure 11.1.
When we created the Log table, we manually filled it with sample data. When the Log
table is in use on the system, we will want the date and time to be automatically added into
the table, so we can use the following to accomplish this:
INSERT INTO
Log ( DateCreated,
Browser,
IPNumber,
CookieID,
WebPageID)
VALUES ( NOW(),
'Mozilla/4.0',
'192.168.1.10',
2,
1)
The above will use the NOW() function to insert the current system date and time into
the newly created row. If you have been keeping up with the examples, this should have cre-
ated a row with the auto-ID of 6, so to view this row we can issue the following query:
117
Search WWH ::




Custom Search