Using Unix Timestamps in MySQL Mini-Course |
This page gives you information on how to easily use Unix Timestamps in MySQL.
Let's first create a simple logging-table and some sample records.
All queries on this page will work with the following table.
-- Table "mytable" DDL CREATE TABLE `mytable` ( `userId` int(11) NOT NULL, `url` varchar(100) NOT NULL, `epoch` int(11) NOT NULL ); INSERT INTO mytable VALUES(1,'homepage',1225540800); # User 1 visited the url homepage on Nov 1, 2008 INSERT INTO mytable VALUES(2,'contact',1225886400); # User 2 visited the url contact on Nov 5, 2008 INSERT INTO mytable VALUES(3,'contact',1225972800); # User 3 visited the url contact on Nov 6, 2008 INSERT INTO mytable VALUES(4,'homepage',1228132800); # User 4 visited the url homepage on Dec 1, 2008
SELECT userId, url, from_unixtime(epoch) FROM mytableThis query outputs:
1 homepage 2008-11-01 13:00:00 2 contact 2008-11-05 13:00:00 3 contact 2008-11-06 13:00:00 4 homepage 2008-12-01 13:00:00You can format your date by using specifiers (look below for a list of specifiers).
SELECT userId, url, from_unixtime(epoch,"%Y-%m-%d") FROM mytableOutput:
1 homepage 2008-11-01 2 contact 2008-11-05 3 contact 2008-11-06 4 homepage 2008-12-01
SELECT DISTINCT from_unixtime(epoch,"%M, %Y") AS month, count(*) as numberOfVisits FROM mytable GROUP BY month ORDER BY epochThis outputs:
November, 2008 3 December, 2008 1This query can be easily changed to get statistics per year, per day, per day of the week, per hour of the day etc. For example, to get yearly stats change the query to:
SELECT DISTINCT from_unixtime(epoch,"%Y") AS year, count(*) as numberOfVisits FROM mytable GROUP BY year ORDER BY epoch
INSERT INTO mytable VALUES(1,'pagename',unix_timestamp(now()))or use YYYY-MM-DD HH:MM:SS :
INSERT INTO mytable VALUES(1,'pagename',unix_timestamp('2008-12-01 12:00:00'))
| Specifier | Description |
| %a | Abbreviated weekday name (Sun..Sat) |
| %b | Abbreviated month name (Jan..Dec) |
| %c | Month, numeric (0..12) |
| %D | Day of the month with English suffix (0th, 1st, 2nd, 3rd, …) |
| %d | Day of the month, numeric (00..31) |
| %e | Day of the month, numeric (0..31) |
| %f | Microseconds (000000..999999) |
| %H | Hour (00..23) |
| %h | Hour (01..12) |
| %I | Hour (01..12) |
| %i | Minutes, numeric (00..59) |
| %j | Day of year (001..366) |
| %k | Hour (0..23) |
| %l | Hour (1..12) |
| %M | Month name (January..December) |
| %m | Month, numeric (00..12) |
| %p | AM or PM |
| %r | Time, 12-hour (hh:mm:ss followed by AM or PM) |
| %S | Seconds (00..59) |
| %s | Seconds (00..59) |
| %T | Time, 24-hour (hh:mm:ss) |
| %U | Week (00..53), where Sunday is the first day of the week |
| %u | Week (00..53), where Monday is the first day of the week |
| %V | Week (01..53), where Sunday is the first day of the week; used with %X |
| %v | Week (01..53), where Monday is the first day of the week; used with %x |
| %W | Weekday name (Sunday..Saturday) |
| %w | Day of the week (0=Sunday..6=Saturday) |
| %X | Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V |
| %x | Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v |
| %Y | Year, numeric, four digits |
| %y | Year, numeric (two digits) |
| %% | A literal '%' character |
Click here for more information on MySQL.com