PHP, Mysql date and time sorting

Source: Internet
Author: User
After one year of work, I have gained a lot. I will make some summary to improve my work efficiency. in my work, mysql uses a UNIX timestamp: the number of seconds from on January 1, January 1, 1970 to the current time, because it is int type, it is very convenient for computer processing, not only the data of php and mysql

After one year of work, I have gained a lot. I will make some summary to improve work efficiency,


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. 'Time') ;}}if ($ seconds >=60) {$ mi = (int) ($ seconds/60); $ seconds = $ seconds % 60; if ($ mi) {$ ret. = ($ mi. 'Points') ;}} if ($ seconds) {$ ret. = ($ seconds. 'second');} return $ ret ;}


/*** Convert the difference timestamp to "1 minute ago ", "3 days ago" and other forms ** @ param timestamp $ ts_diff current time-timestamp to be formatted */public static function formatTime ($ ts_diff) {if ($ ts_diff <= 0) {return date ('Y-m-D');} else if ($ ts_diff <= 3600) {return max (1, (int) ($ ts_diff/60 )). 'minute ago ';} else if ($ ts_diff <= 86400) {return (int) ($ ts_diff/3600 )). 'hour ago ';} else {return (int) ($ ts_diff/86400 )). 'Day before ';}}

/** Convert the week number into a string weekNum2String ($ num) * @ param int * @ return string */public static function weekNum2String ($ num) {switch ($ num) {case 1: return 'Monday'; case 2: return 'Tuesday'; case 3: return 'wedday'; case 4: return 'thurs'; case 5: return 'Friday'; case 6: return 'Saturday'; case 7: return 'Sunday'; default: return 'unknown ';}}



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.