Introduction: You can use these functions to get the date and time of the server running PHP. You can use these functions to format dates and times in many different ways. The most commonly used ones are described in detail below.
date_default_timezone_get — Get the default time zone used by all date and time functions in a script
date_default_timezone_set — Set the default time zone used by 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
?>
Also explain how to set it in the PHP program Time zone method:
//Tianya PHP blog http://blog.phpha.com
date_default_timezone_set('Asia/Shanghai');//'Asia/Shanghai' Asia/Shanghai
date_default_timezone_set ('Asia/Chongqing');//where Asia/Chongqing' is "Asia/Chongqing"
date_default_timezone_set('PRC');//where 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 integer timestamp as given A string generated from a specified format string. If no timestamp is given, the local current time is used. In other words, timestamp is optional and the default value is time().
//The following is the most commonly used case
//Get the year, month, day, hour, minute and second format of 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 // Seconds
[minutes] => 57 // Minutes
[hours] => 16 //Time
[mday] => 6 //Day of the month
[wday] => 2 //Day of the week
[mon] => 11 //Month
[year] => ; 2012 //Year
[yday] => 310 //Day of the year
[weekday] => Tuesday //Day of the week
[month] => November //Month
[0 ] => 1352192237 //Unix timestamp
)
microtime — Returns the current Unix timestamp and microseconds
//Tianya PHP Blog http://blog.phpha.com
date_default_timezone_set('PRC');
//Output 0.35937700 1352192809
echo microtime();
//Get 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 running time is 0.00016188621520996
echo '
?>
strtotime — Parse the date and time description of any English text into a Unix timestamp
[Tianya Note] To be precise, it's not just English, '2012 A numeric string like -11-06 17:00:00′ is also acceptable.
//Tianya PHP Blog http://blog.phpha.com
echo strtotime('2012-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();
?>
The above is taken from the PHP manual [5] – Date /Time date/time function content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!