From PHP Manual [5]-Date/Time function

Source: Internet
Author: User
Introduction: you can use these functions to get the date and time of the server that PHP runs. You can use these functions to format and output the date and time in many different ways. The following sections describe the most common ones.

Introduction: you can use these functions to get the date and time of the server that PHP runs. You can use these functions to format and output the date and time in many different ways. The following sections describe the most common ones.

Date_default_timezone_get-gets the default time zone used by all the datetime functions in a script.
Date_default_timezone_set-sets the default time zone used for all date and time functions in a script.

 
 
  1. date_default_timezone_set('PRC');
  2. echo date('Y-m-d H:i:s') . '
    ';
  3. echo date_default_timezone_get(); // PRC
  4. ?>

The following describes how to set the time zone in the PHP program:

 
 
  1. // Tianya PHP blog http://blog.phpha.com
  2. Date_default_timezone_set ('Asia/Shanghai'); // 'Asia/Shanghai' Asia/Shanghai
  3. Date_default_timezone_set ('Asia/Chongqing '); // The value of Asia/Chongqing is "Asia/Chongqing"
  4. Date_default_timezone_set ('prc'); // PRC is "People's Republic of China"
  5. Ini_set ('date. timezone ', 'etc/GMT-8 ');
  6. Ini_set ('date. timezone ', 'prc ');
  7. Ini_set ('date. timezone ', 'Asia/Shanghai ');
  8. Ini_set ('date. timezone ', 'Asia/Chongqing ');
  9. ?>

Date-format a local time/date

 
 
  1. string date ( string $format [, int $timestamp ] )

Returns the string generated by the integer timestamp according to the given format string. If no timestamp is provided, the current local time is used. In other words, timestamp is optional and the default value is time ().

 
 
  1. // The following are the most common cases
  2. // Obtain the format of year, month, day, hour, minute, and second for the current time
  3. Echo date ('Y-m-d H: I: s ');
  4. ?>

Getdate-get date/time information

 
 
  1. Date_default_timezone_set ('prc ');
  2. $ Row = getdate ();
  3. Print_r ($ row );
  4. ?>
  5. The output is as follows:
  6. // Tianya PHP blog http://blog.phpha.com
  7. Array
  8. (
  9. [Seconds] => 17 // second
  10. [Minutes] => 57 // minute
  11. [Hours] => 16 // Hour
  12. [Mday] => 6 // day of the month
  13. [Wday] => 2 // day of the week
  14. [Mon] => 11 // month
  15. [Year] => 2012 // year
  16. [Yday] => 310 // The Day of the year
  17. [Weekday] => Tuesday // day of the week
  18. [Month] => November // months
  19. [0] = & gt; 1352192237 // Unix timestamp
  20. )

Microtime-returns the current Unix timestamp and number of microseconds

 
 
  1. // Tianya PHP blog http://blog.phpha.com
  2. Date_default_timezone_set ('prc ');
  3. // Output 0.35937700 1352192809
  4. Echo microtime ();
  5. // Obtain the script running time
  6. Function microtime_float ()
  7. {
  8. List ($ usec, $ sec) = explode ('', microtime ());
  9. Return (float) $ usec + (float) $ sec );
  10. }
  11. $ Time_start = microtime_float ();
  12. // Sleep for a while
  13. Usleep (100 );
  14. $ Time_end = microtime_float ();
  15. $ Time = $ time_end-$ time_start;
  16. // The script runtime is 0.00016188621520996
  17. Echo'
    The script running time is '. $ time;
  18. ?>

Strtotime-parses the date and time descriptions of any English text into Unix timestamps
[Tianya note] to be exact, it's not just English. it can be a string of numbers like '2017-11-06 17:00:00.

 
 
  1. // Tianya PHP blog http://blog.phpha.com
  2. Echo strtotime ('2017-11-06 17:00:00 ');
  3. Echo strtotime ("now"), "\ n ";
  4. Echo strtotime ("10 September 2000"), "\ n ";
  5. Echo strtotime ("+ 1 day"), "\ n ";
  6. Echo strtotime ("+ 1 week"), "\ n ";
  7. Echo strtotime ("+ 1 week 2 days 4 hours 2 seconds"), "\ n ";
  8. Echo strtotime ("next Thursday"), "\ n ";
  9. Echo strtotime ("last Monday"), "\ n ";
  10. ?>

Time-returns the current Unix timestamp

 
 
  1. // 1352193513
  2. echo time();
  3. ?>

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.