Php mysql date operation functions

Source: Internet
Author: User

This article uses two examples to analyze and introduce the usage and format of the php date and mysql date, as well as the field types and query efficiency tests used when mysql uses the date.

Php

Int value:
Time (): returns the number of seconds from the Unix epoch (GMT 00:00:00, January 1, January 1, 1970) to the current time.
We can use strtotime () to obtain the number of seconds from January 1-20, 1970 to 2012: strtotime ('2017-2-10 ');
Date value:

String date (string format [, int timestamp])

For date () functions, see http://www.bKjia. c0m/phper/php-function/38146.htm

For example, the current time can be returned directly by date (). Of course, we can specify its format:

For example, date ('Y-m-d', strtotime ('2017-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 ));!! For details, see int strtotime (string time [, int now]).

Date Parameters

The format of each parameter is as follows:
A-"am" or "pm"
A-"am" or "pm"
D-a few days, two digits. If there are less than two digits, fill in the first zero. For example, "01" to "31"
D-the day of the week, with three English letters, for example, "fri"
F-month, full English name; for example: "January"
H-12 hours, for example, "01" to "12"
H-24 hours, for example, "00" to "23"
Hours in the g-12 hour format. Less than two hours do not fill in zero. For example: "1" to 12"
Hours in the g-24-hour format. Less than two hours do not fill in zero. For example: "0" to "23"
I-minutes; for example: "00" to "59"
J-a few days, two digits. If less than two digits are left blank, for example, "1" to "31"
L-the day of the week, full name in English; for example: "Friday"
M-month, two digits. If there are less than two digits, add zero in front, for example, "01" to "12"
N-month, two digits. If less than two digits are left blank, for example, "1" to "12"
M-month, with three English letters, for example, "jan"
S-seconds; for example: "00" to "59"
The end of the s-character is followed by an English sequence, with two English letters, for example, "th", "nd"
T-specifies the number of days in a month, for example, "28" to "31"
U-total seconds
W-number of weeks, for example, "0" (Sunday) to "6" (Saturday)
Y-year, four digits, such as: "1999"
Y-year, two digits, for example, "99"
Z-the day of the year, for example, "0" to "365"
Other characters not in the upper column are listed directly.


(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 ('1970-2-2 '); returns the day of the week.
Select dayofmonth ('1970-2-2 '); returns the day from January 1, 2012.
Select dayofyear ('1970-2-2 '); returns the day of the year.

Similar function: month () day () hour () week ()......
+ Date_add (date, interval 2 days) in a few days );
-Date_sub (date, interval 2 days) in a few days );
Time Format:
Date_format (date, format)
Select DATE_FORMAT ('2017-10-04 22:23:00 ',' % W % M % y ');
Other functions: TIME_TO_SEC () SEC_TO_TIME ()...

At last, let's summarize the comparison of query efficiency among int, varchar, and datetime types in the database.

Test the update of the four tables. update 100 records respectively and record the time: Beijing PHP resource sharing portal d * V .~ X G/QO

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

Table 2: Page running time: 2.5475358963 seconds (fixed length, int time)
Table 3: Page Run Time: 2.45077300072 seconds (varchar, datetime time)

Table 4: Page running time: 2.82798409462 seconds (char, datetime time)


If a large number of queries such as select * from table where time> XX exist in large data volumes, it is meaningful to use int for datetime in MySQL5.1.


Php mysql date comparison code
More better detailed http://www.bKjia. c0m/phper/21/d40b994ac43e630f415940ea4976564c.htm

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.