PHP MySQL date and time function collection _php tips

Source: Internet
Author: User
Tags current time numeric local time modifiers month name php mysql
Collect the Compare full MySQL date and MySQL time function
Date_format (Date,format)
Formats the date value based on the format string. The following modifiers can be used in the format string:%m month name (January ...). December)
%w Week name (Sunday ...) Saturday)
%d The date of the month with English prefix (1st, 2nd, 3rd, and so on). )
%Y years, numbers, 4-bit
%y years, numbers, 2-bit
%a name of the week (Sun ...) Sat)
Days in%d months, numbers (00 ...). 31)
%e the number of days in the month, numbers (0 ...). 31)
%m Month, number (01 ...) 12)
%c month, number (1 ...). 12)
%b abbreviated month name (...) DEC)
%j the number of days in a year (001 ...). 366)
%H hours (00 .....) 23)
%k hours (0 .....) 23)
%h hours (01 .....) 12)
%I Hours (01 .....) 12)
%l hours (1 .....) 12)
%i minutes, Number (00 .....) 59)
%r time, 12 hours (Hh:mm:ss [ap]m)
%T time, 24 hours (HH:MM:SS)
%s seconds (00 ...). 59)
%s seconds (00 ...). 59)
%p am or PM
%w the number of days in one weeks (0=sunday ...). 6=saturday)
%u weeks (0 .....) 52), here Sunday is the first day of the week
%u weeks (0 .....) 52), here Monday is the first day of the week
Percent% of a text "%".

All other characters do not interpret are copied into the result.

Mysql> Select Date_format (' 1997-10-04 22:23:00 ', '%w%m%Y ');
-> ' Saturday October 1997 '
Mysql> Select Date_format (' 1997-10-04 22:23:00 ', '%h:%i:%s ');
-> ' 22:23:00 '
Mysql> Select Date_format (' 1997-10-04 22:23:00 ',
'%d%y%a%d%m%b%j ');
-> ' 4th Sat Oct 277 '
Mysql> Select Date_format (' 1997-10-04 22:23:00 ',
'%H%k%I%r%T%s%w ');
-> ' 10:23:00 PM 22:23:00 00 6 '
In MySQL3.23,% is required before formatting modifiers. In the earlier version of MySQL,% is optional.

Time_format (Time,format)
This is used like the Date_format () function above, but the format string can contain only those formatting modifiers that handle hours, minutes, and seconds.

Other modifiers produce a null value or 0.
Curdate ()

Current_date
Returns the Today date value in ' Yyyy-mm-dd ' or YYYYMMDD format, depending on whether the function is used in a string or a numeric context.
Mysql> select Curdate ();
-> ' 1997-12-15 '
Mysql> Select curdate () + 0;
-> 19971215

Curtime ()

Current_time
Returns the current time value in ' HH:MM:SS ' or HHMMSS format, depending on whether the function is used in a string or in the context of a number.
Mysql> select Curtime ();
-> ' 23:50:26 '
Mysql> Select Curtime () + 0;
-> 235026

Now ()

Sysdate ()

Current_timestamp
Returns the current date and time in the ' Yyyy-mm-dd HH:MM:SS ' or YYYYMMDDHHMMSS format, depending on whether the function is in a string or a numeric
Context is used.
Mysql> Select Now ();
-> ' 1997-12-15 23:50:26 '
Mysql> Select now () + 0;
-> 19971215235026

Unix_timestamp ()

Unix_timestamp (date)
Returns a UNIX timestamp (the number of seconds since ' 1970-01-01 00:00:00 ' GMT) if no parameter calls are invoked. If Unix_timestamp () is invoked with a date parameter, it returns the number of seconds that start from ' 1970-01-01 00:00:00 ' GMT. Date can be a date string, a DateTime string, a timestamp, or a number of local time in YYMMDD or YYYYMMDD format.
Mysql> select Unix_timestamp ();
-> 882226357
Mysql> Select Unix_timestamp (' 1997-10-04 22:23:00 ');
-> 875996580

When Unix_timestamp is used in a timestamp column, the function will accept the value directly, without the implied "String-to-unix-timestamp" transformation http://www.knowsky.com/.

From_unixtime (Unix_timestamp)
Returns the value represented by the Unix_timestamp parameter in ' Yyyy-mm-dd HH:MM:SS ' or YYYYMMDDHHMMSS format, depending on whether the function is used in a string or a numeric context.
Mysql> Select From_unixtime (875996580);
-> ' 1997-10-04 22:23:00 '
Mysql> Select From_unixtime (875996580) + 0;
-> 19971004222300

From_unixtime (Unix_timestamp,format)
Returns a String representing the Unix time token, formatted according to the format string. FORMAT can contain the same modifiers as the entries listed in the Date_format () function.
Mysql> Select From_unixtime (Unix_timestamp (),
'%Y%d%m%h:%i:%s%x ');
-> ' 1997 23rd December 03:43:30 X '

Sec_to_time (seconds)
Returns the seconds parameter, changing to hours, minutes, and seconds, and the value is formatted as ' HH:MM:SS ' or HHMMSS, depending on whether the function is used in a string or in a numeric context.
Mysql> Select Sec_to_time (2378);
-> ' 00:39:38 '
Mysql> Select Sec_to_time (2378) + 0;
-> 3938

Time_to_sec (Time)
Returns the time parameter, converted to seconds.
Mysql> Select Time_to_sec (' 22:23:00 ');
-> 80580
Mysql> Select Time_to_sec (' 00:39:38 ');
-> 2378
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.