PHPMysql date and time sorting

Source: Internet
Author: User
Tags echo date
Welcome to the Linux community forum and interact with 2 million technical staff. mysql is used at work at a UNIX timestamp: the number of seconds from on January 1, January 1, 1970 to the current time, because it is an int type, it is very convenient for computer processing. It is not only a format of data interaction between php and mysql, but also a data interaction between various clients.

Welcome to the Linux community forum and interact with 2 million technical staff> at work, mysql uses a UNIX timestamp: the number of seconds from on January 1, January 1, 1970 to the current time, because it is an int type, it is very convenient for computer processing. It is not only a format of data interaction between php and mysql, but also a data interaction between various clients.

Welcome to the Linux community forum and interact with 2 million technicians>

In work, mysql uses a UNIX timestamp: the number of seconds from on January 1, January 1, 1970 to the current time. Because it is of the int type, it is very convenient for computer processing, it is not only a format for data interaction between php and mysql, but also a standard for data interaction (android/IOS) on various clients. Therefore, if you only save and display the date, the UNIX timestamp should be used to calculate the date and use it as the standard date format.

The common process is to convert the time of an HTML page into a timestamp and save it to mysql. The timestamp is retrieved from mysql and displayed on the web or mobile client. In short, the time saved in mysql is the UNIX timestamp, which is then formatted as the appropriate time by PHP.

Several common functions are introduced.

1. date (), 2. mktime (), 3. getdate (), 4. strftime ()

1. date ()

Get time and date in PHP

Use the date () function to convert the timestamp or current time to a formatted string, for example:

Echo date ('Y-I-S'); // output

2. mktime ()

Use mktime () to convert a time to a UNIX Timestamp

$ Timestamp = mktime ();

There are three methods to obtain the current timestamp:

Mktime (), time (), date ('U ')

Mktime for Time Calculation

Mktime (12, 0, 0, $ mon, $ day + 10, $ year); timestamp after ten days

3. getdate () function:

$ Today = getdate ();

Print_r ($ today );

// Output

Array

(

[Seconds] => 38

[Minutes] => 38

[Hours] => 22

[Mday] => 25

[Wday] => 2

[Mon] => 3

[Year] = & gt; 2014

[Yday] => 83

[Weekday] => Tuesday

[Month] => March

[0] = & gt; 1395758318

)

Use the checkdate () function to verify the date validity

4. strftime ()

Format Timestamp

Mysql formatting time

1. DATE_FORMAT ()

2. UNIX_TIMESTAMP () returns the date formatted as a UNIX timestamp, for example, SELECT UNIX_TIMESTAMP (date) FROM table, which can be processed in PHP.

There are few formatting time functions in PHP. Several Common formatting time functions are introduced.

/**

*

* Convert the timestamp time to x hour x minute x second

*

*/

Public static function getTimeLong ($ seconds ){

If (! $ Seconds ){

Return '0 second ';

}

$ Ret = '';

If ($ seconds> = 3600 ){

$ Hours = (int) ($ seconds/3600 );

$ Seconds =$ seconds % 3600;

If ($ hours ){

$ Ret. = ($ hours. '');

}

}

If ($ seconds> = 60 ){

$ Mi = (int) ($ seconds/60 );

$ Seconds = $ seconds % 60;

If ($ mi ){

$ Ret. = ($ mi. 'quantity ');

}

}

If ($ seconds ){

$ Ret. = ($ seconds. 'second ');

}

Return $ ret;

}

/**

[1] [2]

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.