time()函数是PHP中的内置函数,它返回自Unix纪元以来以秒为单位的当前时间。可以使用PHP中的date()函数将秒数转换为当前日期。
用法:
int time()
参数:如上所示,该功能不接受任何参数。
返回值:此函数返回自Unix Epoch以来以秒为单位的当前时间。
注意:程序的所有输出均与撰写本文的日期相对应。
以下示例程序旨在说明time()函数:
示例1:下面的程序以秒为单位打印当前时间。
// PHP program to demonstrate the use of current
// time in seconds since Unix Epoch
// variable to store the current time in seconds
$currentTimeinSeconds = time();
// prints the current time in seconds
echo $currentTimeinSeconds;
?>
输出:
1525376494
示例2:下面的程序以日期格式打印当前时间。
// PHP program to demonstrate the use of current
// date since Unix Epoch
// variable to store the current time in seconds
$currentTimeinSeconds = time();
// converts the time in seconds to current date
$currentDate = date('Y-m-d', $currentTimeinSeconds);
// prints the current date
echo ($currentDate);
?>
输出:
2018-05-03