如何返回过去的年数?
我和我的朋友正在为 IRC 机器人编写一个相当基本的正常运行时间脚本。
这是我们的代码:
function Uptime()
{
global $uptimeStart;
$currentTime = time();
$uptime = $currentTime - $uptimeStart;
$this->sendIRC("PRIVMSG {$this->ircChannel} :Uptime: ".date("z",$uptime)." Day(s) - ".date("H:i:s",$uptime));
}
$uptimeStart 在脚本运行时立即设置,如 time();
由于某种原因,当我执行此函数时,它从 364 天零 19 小时开始。我不明白为什么。
My friend and I are working on a fairly basic uptime script for an IRC Bot.
Here's our code:
function Uptime()
{
global $uptimeStart;
$currentTime = time();
$uptime = $currentTime - $uptimeStart;
$this->sendIRC("PRIVMSG {$this->ircChannel} :Uptime: ".date("z",$uptime)." Day(s) - ".date("H:i:s",$uptime));
}
$uptimeStart is set immediately when the script runs, as time();
for some reason when I execute this function, it starts at 364 days and 19 hours. I can't figure out why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的
$uptime
不是应在date()
中使用的时间戳,而是时间差。你有一定的秒数,而不是时间戳(与实际日期相对应。只需使用类似这样的东西来计算(快速计算,为 1 天、2 小时等添加一些额外的大脑);)
等等
Your
$uptime
is not a timestamp as should be used indate()
, but a difference in time. You have an amount of seconds there, not a timestamp (that corresponds with an actual date.just use something like this to cacluate (quick one, put some extra brain in for things like 1 day, 2 hours etc) ;)
etc
如果您有 5.3 或更高版本,请使用 DateTime 和 DateInterval 类:
If you have 5.3 or above, use the DateTime and DateInterval classes:
通过在该时差上调用
date()
不会得到任何有意义的信息。您应该将这个时间差逐渐除以年、月、日、小时,所有这些都以秒为单位。这样您就可以得到这些条件下的时差。..等等以获得小时和分钟。
You won't get anything meaninful by calling
date()
on that time difference. You should take that time difference and progressively divide with years, months, days, hours, all measured in seconds. That way you'll get what the time difference in those terms... etc to get hours and minutes.
第二个参数设置为 0 的 date() 函数实际上会返回您(零日期 + (您的时区)),其中“零日期”是“00:00:00 1970-01-01”。看起来您的时区是 UTC-5,因此您得到 (365 天 24 小时) - (5 小时) = (364 天 19 小时)
另外, date() 函数并不是显示两个日期之间差异的最佳方式。查看其他答案 - 已经发布了计算年份之间差异的好方法
date() function with second argument set to 0 will actually return you (zero-date + (your time zone)), where "zero-date" is "00:00:00 1970-01-01". Looks like your timezone is UTC-5, so you get (365 days 24 hours) - (5 hours) = (364 days 19 hours)
Also, date() function is not the best way to show the difference between two dates. See other answers - there are are already posted good ways to calculate difference between years