SELECT DATE_FORMAT( NOW() , '%Y-%c-%d') AS mydate_fmt;
If you want to run the above formatting against a timestamp field that is stored in your table:
select date_format( `myfield` , '%Y-%c-%d') as myfield_fmt from mytable;
There are more date formatter parameters that you can use and here's the current list as per the MySQL documentation:
Source: http://dev.mysql.com/doc/r
%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
%x x, for any “x” not listed above
SELECT UNIX_TIMESTAMP();
This leads me to another point I would like to mention before more code.
How to store a date in the database. I highly recommend using the timestamp, date or datetime field types. The reason for this is that although your database may need a specific date format to understand what it's being fed, it can't calculate against those values without having to use a conversion back into a workable data type - which usually involves lines of PHP code, then feed it back to be calculated, and then it needs to be converted AGAIN to output a nicely formatted date. This also facilitates date formatting such as the example I provided above against a field. Speaking from years of experience, I would NEVER use a unix timestamp. Sure, it's small in comparison to a datetime, or timestamp field, but did you know that a unix timestamp will only go back to January 1, 1970 (otherwise known as the Unix Epoch)? So you're out of luck when trying to calculate an age if your application demographics include people older than early 40. That's not good.
SELECT DATE_ADD( NOW( ) , INTERVAL 5 DAY ) as mydate_fmt;
Subtract 5 days, is as simple as:
SELECT DATE_SUB( NOW( ) , INTERVAL 5 DAY ) as mydate_fmt;
To add hours or minutes:
SELECT DATE_ADD( NOW( ) , INTERVAL 10 MINUTE ) as mydate_fmt;
SELECT DATE_ADD( NOW( ) , INTERVAL 28 HOUR ) as mydate_fmt;
And the really cool thing when doing date calculations is that you can wrap those functions in formatting functions too, or you can do those calculations against fields already in your tables:
SELECT DATE_FORMAT( date_add(now(), interval 10 day), '%Y-%c-%d') as mydate_fmt;
SELECT DATE_FORMAT( date_add( `myfield`, interval 10 day), '%Y-%c-%d') as myfield_fmt from mytable;
As you can see, with a single line of simple MySQL query, I'm achieving what would take 10 lines of PHP, (or more) can do. Plus I have so much flexibility.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Author
Commented:Have a great day.