PHP MySQL date operation function _php Tutorial

Source: Internet
Author: User
Tags php mysql
In this paper, two examples are used to analyze the usage and format of PHP date and MySQL date, as well as the field types and query efficiency tests that include the MySQL usage date.

Php

int value:
Time (): is the number of seconds to return from the Unix era (00:00:00 GMT, January 1, 1970) to the current.
The number of seconds we want to get from January 1, 1970 to 2012-2-10 can be achieved by Strtotime (): Strtotime (' 2012-2-10 ');
Date value:

String date (string format [, int timestamp])

Refer to the date () function for http://www.bKjia.c0m/phper/php-function/38146.htm

For example: direct date () returns the implementation of the current time, of course we can specify his format:

For example date (' y-m-d ', strtotime (' 2012-2-10 '));
Time Operation:

The code is as follows Copy Code
Date (' y-m-d h:i:s ', Strtotime (' +1 Week '));
Date (' y-m-d h:i:s ', Strtotime (' +5 hours '));
Date (' y-m-d h:i:s ', Strtotime (' next Monday));
Date (' y-m-d h:i:s ', Strtotime (' last Sunday '));
Date (' y-m-d h:i:s ', strtotime (' + 1 day ', 12313223));!! See int Strtotime (string time [, int now])

Parameters for Date

The format of each parameter is represented by:
A-"AM" or "PM"
A-"AM" or "PM"
D-A few days, two digits, if less than two is the front 0; such as: "01" to "31"
D-Days of the week, three English letters; such as: "Fri"
F-month, full name in English; such as: "January"
H-12 hours of the hour; such as: "01" to "12"
H-24 hours of the hour; such as: "00" to "23"
G-12 hours of the hour, less than two digits do not fill 0; such as: "1" to 12 "
G-24 hours of the hour, less than two digits do not fill 0; such as: "0" to "23"
I-minute; such as: "00" to "59"
J-A few days, two digits, if less than two bits do not fill 0; such as: "1" to "31"
L-Day of the week, English full name; such as: "Friday"
M-month, two digits, if less than two digits in front of 0; such as: "01" to "12"
N-month, two digits, if less than two digits does not fill 0; such as: "1" to "12"
M-month, three letters of English; such as: "Jan"
S-second; such as: "00" to "59"
S-End with English ordinal number, two English letters; such as: "th", "nd"
T-the number of days in the specified month; such as: "28" to "31"
U-Total number of seconds
W-Number of days of the week, such as: "0" (Sunday) to "6" (Saturday)
Y-year, four digits; such as: "1999"
Y-year, two digits; such as: "99"
Z-the day ordinal of a year; such as: "0" to "365"
Other characters that are not listed directly list the character


(2) MySQL:

The code is as follows Copy Code
Int->datetime
Select From_unixtime (int_time) from table;
datetime->int;
Select Unix_timestamp (date_time) from table;

Time Operation:

The code is as follows Copy Code
Select DayOfWeek (' 2012-2-2 '); Returns the day of the week one
Select DayOfMonth (' 2012-2-2 '); Returns the day ordinal of January
Select DayOfYear (' 2012-2-2 '); Returns the day ordinal of a year

Similar functions: Month () Day () hour () Week () ...
+ Date_add (Date,interval 2 days);
-Date_sub (Date,interval 2 days);
Time Format:
Date_format (Date,format)
Select Date_format (' 1997-10-04 22:23:00 ', '%W%M%Y ');
Other functions: Time_to_sec () sec_to_time () ...

Finally, we summarize the use of time in the database is to use int, varchar, datetime these kinds of query efficiency comparison bar.

Test updates for four tables, update 100 records separately, and record time: Beijing PHP resource sharing portal D*v.~x G/qo

Table One: Page run time: 2.62180089951 seconds (non-fixed length, int time) Beijing PHP resource sharing portal

Table II: Page run time: 2.5475358963 seconds (fixed length, int time)
Table Three: Page run time: 2.45077300072 seconds (varchar,datetime time)

Table four: Page run time: 2.82798409462 seconds (char,datetime time)


With large data volumes, if there is a large number of select * from table where time >xx such a query, it makes sense to use int for datetime when MySQL5.1.


PHP MySQL Date comparison code
More and better detail http://www.bKjia.c0m/phper/21/d40b994ac43e630f316940ea4976564c.htm

http://www.bkjia.com/PHPjc/632937.html www.bkjia.com true http://www.bkjia.com/PHPjc/632937.html techarticle In This paper, two examples are used to analyze the usage and format of PHP date and MySQL date, as well as the field types and query efficiency tests that include the MySQL usage date. PHP int Value ...

  • 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.