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.
- date_default_timezone_set('PRC');
- echo date('Y-m-d H:i:s') . '
';
- echo date_default_timezone_get(); // PRC
- ?>
The following describes how to set the time zone in the PHP program:
- // Tianya PHP blog http://blog.phpha.com
- Date_default_timezone_set ('Asia/Shanghai'); // 'Asia/Shanghai' Asia/Shanghai
- Date_default_timezone_set ('Asia/Chongqing '); // The value of Asia/Chongqing is "Asia/Chongqing"
- Date_default_timezone_set ('prc'); // PRC is "People's Republic of China"
- Ini_set ('date. timezone ', 'etc/GMT-8 ');
- Ini_set ('date. timezone ', 'prc ');
- Ini_set ('date. timezone ', 'Asia/Shanghai ');
- Ini_set ('date. timezone ', 'Asia/Chongqing ');
- ?>
Date-format a local time/date
- 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 ().
- // The following are the most common cases
- // Obtain the format of year, month, day, hour, minute, and second for the current time
- Echo date ('Y-m-d H: I: s ');
- ?>
Getdate-get date/time information
- Date_default_timezone_set ('prc ');
- $ Row = getdate ();
- Print_r ($ row );
- ?>
- The output is as follows:
- // Tianya PHP blog http://blog.phpha.com
- Array
- (
- [Seconds] => 17 // second
- [Minutes] => 57 // minute
- [Hours] => 16 // Hour
- [Mday] => 6 // day of the month
- [Wday] => 2 // day of the week
- [Mon] => 11 // month
- [Year] => 2012 // year
- [Yday] => 310 // The Day of the year
- [Weekday] => Tuesday // day of the week
- [Month] => November // months
- [0] = & gt; 1352192237 // Unix timestamp
- )
Microtime-returns the current Unix timestamp and number of microseconds
- // Tianya PHP blog http://blog.phpha.com
- Date_default_timezone_set ('prc ');
- // Output 0.35937700 1352192809
- Echo microtime ();
- // Obtain the script running time
- Function microtime_float ()
- {
- List ($ usec, $ sec) = explode ('', microtime ());
- Return (float) $ usec + (float) $ sec );
- }
- $ Time_start = microtime_float ();
- // Sleep for a while
- Usleep (100 );
- $ Time_end = microtime_float ();
- $ Time = $ time_end-$ time_start;
- // The script runtime is 0.00016188621520996
- Echo'
The script running time is '. $ time;
- ?>
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.
- // Tianya PHP blog http://blog.phpha.com
- Echo strtotime ('2017-11-06 17:00:00 ');
- Echo strtotime ("now"), "\ n ";
- Echo strtotime ("10 September 2000"), "\ n ";
- Echo strtotime ("+ 1 day"), "\ n ";
- Echo strtotime ("+ 1 week"), "\ n ";
- Echo strtotime ("+ 1 week 2 days 4 hours 2 seconds"), "\ n ";
- Echo strtotime ("next Thursday"), "\ n ";
- Echo strtotime ("last Monday"), "\ n ";
- ?>
Time-returns the current Unix timestamp
- // 1352193513
- echo time();
- ?>