Database Reference
In-Depth Information
Figure 11.13
The number of days since year 0.
Figure 11.13 shows this function running within a SELECT query.
Again, this can be useful when you need to perform some calculation using dates. For
instance, our cookie table contains the date that the cookie was created, which is the first
day that the user would have accessed the site. The following query could be used to ascer-
tain how many days have passed since that first access:
SELECT
NOW() AS todaysdate,
TO_DAYS(NOW())-TO_DAYS (datecreated) AS dayssince
FROM
cookies
Figure 11.14 shows the query running. You'll notice that it has been nearly a year since
the creation of most of the cookies.
On a webpage, you would probably want to restrict that query to a single row by select-
ing the ID of the user's cookie with a WHERE statement, for instance:
SELECT
NOW() AS todaysdate,
TO_DAYS(NOW())-TO_DAYS (datecreated) AS dayssince
FROM
cookies
WHERE
ID=1
Figure 11.1 4
TO_DAYS() used for date calculation.
Search WWH ::




Custom Search