要在PHP中添加时间,您可以使用date()
函数和strtotime()
函数。
以下是一个示例,演示如何在当前时间上添加一小时:
$current_time = date("Y-m-d H:i:s"); // 获取当前时间
$future_time = date("Y-m-d H:i:s", strtotime($current_time . "+1 hour")); // 在当前时间上添加一小时
echo "当前时间:".$current_time."<br>";
echo "一小时后的时间:".$future_time;
输出:
当前时间:2021-01-01 12:00:00
一小时后的时间:2021-01-01 13:00:00
在上面的示例中,strtotime()
函数将当前时间$current_time
与字符串"+1 hour"
相结合,返回一个包含一小时后时间的时间戳。然后,date()
函数将该时间戳格式化为所需的日期时间格式。
您可以根据需要修改时间间隔,例如"+1 day"
表示添加一天,"+1 week"
表示添加一周,以此类推。
上一篇:php封装js方法(php封包)
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站