如何在php中使用时间函数

2023-06-13,

如何在php中使用时间函数?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

php中有unix时间戳的 相关操作函数,使用很方便

time() 返回当前的 Unix 时间戳

microtime -- 返回当前 Unix 时间戳和微秒数

例 1. 用 microtime() 对脚本的运行计时

<?php
/**
* Simple function to replicate PHP 5 behaviour
*/
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;
echo "Did nothing in $time seconds/n";
?>

mktime()取得一个日期的 Unix 时间戳

int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )

参数可以从右向左省略,任何省略的参数会被设置成本地日期和时间的当前值

date()格式化一个本地时间/日期

string date ( string format [, int timestamp] )

提示: 自 PHP 5.1 起在 $_SERVER['REQUEST_TIME'] 中保存了发起该请求时刻的时间戳。

strtotime -- 将任何英文文本的日期时间描述解析为 Unix 时间戳

echo strtotime("+1 day"), "/n";
echo strtotime("+1 week"), "/n";

例2. 某个时间的后一天,后一月

strtotime("+1 day ".$day);
strtotime("2008-01-31 +1 month");
strtotime($day." +1 day");

看完上述内容,你们掌握如何在php中使用时间函数的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注本站行业资讯频道,感谢各位的阅读!

《如何在php中使用时间函数.doc》

下载本文的Word格式文档,以方便收藏与打印。