Reference : Date Formats

PHP

Component -> UNIX timestamp (seconds)

int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )

UNIX timestamp (seconds) -> Component

array getdate ( [int timestamp] )
Key Description Example returned values
"seconds" Numeric representation of seconds 0 to 59
"minutes" Numeric representation of minutes 0 to 59
"hours" Numeric representation of hours 0 to 23
"mday" Numeric representation of the day of the month 1 to 31
"wday" Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
"mon" Numeric representation of a month 1 through 12
"year" A full numeric representation of a year, 4 digits Examples: 1999 or 2003
"yday" Numeric representation of the day of the year 0 through 365
"weekday" A full textual representation of the day of the week Sunday through Saturday
"month" A full textual representation of a month, such as January or March January through December
0 Seconds since the Unix Epoch, similar to the values returned by time() and used by date(). System Dependent, typically -2147483648 through 2147483647.

UNIX timestamp (seconds) -> Formatted string

string date ( string format [, int timestamp] )

Javascript

Date object constructors

Date()                                          //Use the current date and time
Date(dateString)                                //String format is "month day, year hours:minutes:seconds".
Date(year, month, day)                          //Year is 0 to 99, Month is 0 to 11, Day is 1 to 31
Date(year, month, day, hours, minutes, seconds)

Date object -> components

d.getHours();    //0-23
d.getMinutes();  //0-59
d.getSeconds();  //0-59
d.getMonth();    //0-11
d.getDate();     //1-31
d.getDay();      //0-6
d.getYear();     //Years since 1900
d.getFullYear(); //YYYY
d.getTime();     //ms since epoch

Date object -> UNIX timestamp (milliseconds)

d.getTime();

UNIX timestamp (milliseconds) -> Date object

pre. var d = new Date();
d.setTime(...);

MySQL

See also MySQL Manual - Date-Time functions

Passing dates to MySQL

INSERT INTO datetable (adate) VALUES ('2006-06-06');
INSERT INTO timetable (atime) VALUES ('21:55:01');
INSERT INTO bothtable (atdat) VALUES ('2006-06-06 21:55:01');

Data Types

  • DATE ''YYYY-MM-DD ('1000-01-01' to '9999-12-31')''. Can be assigned as string or number.
  • TIME ''HH:MM:SS ('838:59:59' to '838:59:59')''. Can be assigned as string or number.
  • DATETIME ''YYYY-MM-DD HH:MM:SS ('1000-01-01 00:00:00' to '9999-12-31 23:59:59')''. Can be assigned as string or number.
  • TIMESTAMP ''YYYY-MM-DD HH:MM:SS ('1970-01-01 00:00:00' to '2037-something')''. +0 to get as number

Functions

  • ''CURDATE()'' returns 'YYYY-MM-DD' or YYYYMMDD.
  • ''CURTIME()'' returns 'HH:MM:SS' or HHMMSS.
  • ''DATE(expr)'' returns the DATE part of expr.
  • ''DAY(date)'', ''DAYOFMONTH(date)'' 0..31
  • ''DAYNAME(date)''
  • ''DAYOFWEEK(date)'' 1(Sunday)..7(Saturday)
  • ''FROM_UNIXTIME(timestamp)'', ''FROM_UNIXTIME(timestamp, fmt)'' timestamp is seconds since epoch
  • ''DATE_FORMAT(date,fmt)''
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
%x x, for any 'x' not listed above

Ranges for the month and day specifiers begin with zero due to the fact that MySQL allows the storing of incomplete dates such as '2004-00-00'.