Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc SQL Scripts & Samples Links Database Forum DBA Videos
internet.com

» Database Journal Home
» Database News
» DBA Videos
» Database Articles
» Database Tutorials
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» DBA Jobs
» Sitemap

News Via RSS Feed



follow us on Twitter

Marketplace Partners
Be a Marketplace Partner




internet.commerce
Be a Commerce Partner


















FCC's Martin: Open Networks Becoming the Norm

Enterprise SaaS Buyers Want More Than Uptime

Cuban Waves Off SEC Allegations

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Database Journal | DBA Support | SQLCourse | SQLCourse2 | Swynk







Senior Systems Engineer - UNIX (CO)
Next Step Systems
US-CO-Thornton

Justtechjobs.com Post A Job | Post A Resume

Featured Database Articles

MySQL

April 15, 2003

MySQL date and time functions, Part 2

By Ian Gilfillan

Date formats

The DATE_FORMAT function is a powerful function that allows you to return a specified date in a number of different ways. For example, those who use American time (MM-DD-YYYY, as you can see at the beginning of this article) instead of the standard International time, this function provides a painless way to convert. Below is a list of format specifiers:

%aAbbreviation of the day (from Sun-Sat)
%bAbbreviation of the month (from Jan-Dec)
%cNumeric month (from 1-12)
%DNumeric day of the month with suffix (1st, 2nd, and so on)
%dNumeric day of the month with two digits(from 00-31)
%eNumeric day of the month with one or two digits(from 0-31)
%HHour (from 00-23)
%hHour (from 01-12)
%iMinutes (from 00-59)
%IHour (from 01-12)
%jDay of the year (from 001-366)
%kHour with one or two digits (from 0-23)
%lHour with one digit (from 1-12)
%MMonth name (from January-December)
%mNumeric month (from 01-12)
%pA.M. or P.M.
%r12-hour time (hh:mm:ss A.M.or P.M.)
%SSeconds (from 00-59)
%sSeconds (from 00-59)
%T24 hour time (hh:mm:ss)
%UWeek (from 00-53, Sunday being the first day of the week)
%uWeek (from 00-53, Monday being the first day of the week)
%VWeek (from 01-53, Sunday being the first day of the week)
%vWeek (from 01-53, Monday being the first day of the week)
%WName of the day in the week (from Sunday-Saturday)
%wDay of the week (from 0 - Sunday, to 6 - Saturday)
%XFour-digit numeric year for the week (Sunday being the first day of the week)
%xFour-digit numeric year for the week (Monday being the first day of the week)
%YFour-digit numeric year
%yTwo-digit numeric year
%%Percentage sign (escaped)
Let's look at converting a standard date into a format used in the US.
mysql> SELECT DATE_FORMAT('2003-07-14','%b %d,%Y');
+--------------------------------------+
| DATE_FORMAT('2003-07-14','%b %d,%Y') |
+--------------------------------------+
| Jul 14,2003                          |
+--------------------------------------+
A subset of this function is the TIME_FORMAT() function, which is identical, but allows you to use those formats to do with time.
The last function we are going to look at specifically will be meaningful to those of you familiar with the concept of Unix time. Unix time is the time in seconds since midnight 1 January 1970, and is used by many applications. The UNIX_TIMESTAMP() function returns the Unix time of the current time When called without a parameter, or converts a specified date if one is supplied.
mysql> SELECT UNIX_TIMESTAMP();
+------------------+
| UNIX_TIMESTAMP() |
+------------------+
|       1050267998 |
+------------------+

mysql> SELECT UNIX_TIMESTAMP('2003-07-14');
+------------------------------+
| UNIX_TIMESTAMP('2003-07-14') |
+------------------------------+
|                   1058133600 |
+------------------------------+
Looking at the possibilities available with this function, along with the date and time calculations we've looked at already, you can see there's not always that much need to do this inside the application!

Below is a reference for the MySQL date and time functions. Once you have mastered what we've covered so far, none of the other functions will present anything tricky, but you should give the list a read through - you never know when you'll need them.

Go to page: Prev  1  2  3  Next  

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MySQL Archives







Latest Forum Threads
MySQL Forum
Topic By Replies Updated
Can't connect to MySQL server on 'localhost' (10061) paperclip 1 November 18th, 11:11 PM
Mysql database import problem deisel79 1 November 18th, 11:00 PM
Being the Stored Procedure woes of a foreigner in the land of MySQL sim303 0 November 13th, 07:52 PM
Help with a confusing SELECT Query.... Quadcom 2 November 12th, 02:03 PM








internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers