Php,mysql Date and time finishing _php tutorials

Source: Internet
Author: User
Tags echo date php and mysql time and date
Work a year, harvest a lot, slowly do some summary, improve work efficiency,


The time to work with MySQL is a Unix timestamp: from January 1, 1970 0 o'clock to the current time in seconds, due to the int type, it is convenient for computer processing, not only PHP and MySQL data interaction of a format, in various clients, It is also the standard for data interaction (Android/ios), so if you just save and display the date, you should use the UNIX timestamp to calculate the date and the standard date format.


The common process of working is to convert the time of the HTML page into a timestamp saved to MySQL, and the timestamp from MySQL is formatted to appear on the Web or mobile client. In short, the time saved in MySQL is the Unix timestamp, and then the PHP format to the appropriate time


Introduce a few common functions


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


1.date ()

Get time and date in PHP

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

echo Date (' y-i-s ');//Output 2014-3-25


2.mktime ()

Convert time to UNIX timestamp using mktime ()

$timestamp = Mktime ();

There are three ways to get the current timestamp:

Mktime (), Time (), date (' U ')

Mktime Doing time arithmetic

Mktime (12,0,0, $mon, $day +10, $year); time stamp 10 days later


3.getdate () function:

$today = getdate ();

Print_r ($today);

Output

Array
(
[Seconds] = 38
[Minutes] = 38
[Hours] = 22
[Mday] = 25
[Wday] = 2
[Mon] = 3
[Year] = 2014
[Yday] = 83
[Weekday] = Tuesday
[Month] = March
[0] = 1395758318
)

Use the Checkdate () function to verify date validity


4.strftime ()

Formatting timestamps

MySQL format time

1.date_format ()

2.unix_timestamp () returns the date formatted as a UNIX timestamp, for example: SELECT unix_timestamp (date) from table so that it can be processed in PHP


The function of formatting time in PHP is relatively small, and several commonly used format time functions are introduced.

/** *  * Convert timestamp time to x x x seconds * */public    static function Gettimelong ($seconds) {        if (! $seconds) {            Return ' 0 seconds ';        }        $ret = ";        if ($seconds >= 3600) {            $hours = (int) ($seconds/3600);            $seconds = $seconds% 3600;            if ($hours) {                $ret. = ($hours. ' time ');            }        }        if ($seconds >=) {            $mi = (int) ($seconds/60);            $seconds = $seconds%;            if ($mi) {                $ret. = ($mi. ' min ');            }        }        if ($seconds) {            $ret. = ($seconds. ' seconds ');        }        return $ret;    }


/** * Convert the difference timestamp to "1 minutes Ago", "3 days Ago" and other forms * * @param timestamp $ts _diff Current time-timestamp */public static function Formatti to be formatted Me ($ts _diff) {if ($ts _diff <=0) {return date (' y-m-d ');} else if ($ts _diff <= 3600) {return Max (1, (int) ($ts _diff/60)). ' Minutes ago ';} else if ($ts _diff <= 86400) {return ((int) ($ts _diff/3600)). ' Hour ago ';} Else{return ((int) ($ts _diff/86400)). ' days ago ';}}

    /** converts a number week to a string week weeknum2string ($num)     * @param int     * @return String */public    static function Weeknum2string ($num) {        switch ($num) {case            1:                return ' Monday ';            Case 2:                return ' Tuesday ';            Case 3:                return ' Wednesday ';            Case 4:                return ' Thursday ';            Case 5:                return ' Friday ';            Case 6:                return ' Saturday ';            Case 7:                return ' Sunday ';            Default:                return ' unknown ';        }    }



http://www.bkjia.com/PHPjc/755788.html www.bkjia.com true http://www.bkjia.com/PHPjc/755788.html techarticle work a year, harvest a lot, slowly do some summary, improve work efficiency, working in MySQL time is a Unix timestamp: from January 1, 1970 0 o'clock to the current time of the number of seconds ...

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