添加 'x' 小时数 [英] Add 'x' number of hours to date

查看:27
本文介绍了添加 'x' 小时数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有 php 返回当前日期/时间,如下所示:

I currently have php returning the current date/time like so:

$now = date("Y-m-d H:m:s");

我想做的是有一个新的变量 $new_time 等于 $now + $hours,其中 $hours 是一个小时数从 24 到 800 不等.

What I'd like to do is have a new variable $new_time equal $now + $hours, where $hours is a number of hours ranging from 24 to 800.

有什么建议吗?

推荐答案

你可以使用类似strtotime() 函数向当前时间戳添加一些内容.$new_time = date("Y-m-d H:i:s", strtotime('+5 hours')).

如果你在函数中需要变量,你必须使用双引号,然后像 strtotime("+{$hours} hours"),但是最好使用 strtotime(sprintf("+%d 小时", $hours)) 然后.

If you need variables in the function, you must use double quotes then like strtotime("+{$hours} hours"), however better you use strtotime(sprintf("+%d hours", $hours)) then.

这篇关于添加 'x' 小时数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆