如何返回过去的年数?

发布于 2024-10-13 21:51:40 字数 416 浏览 8 评论 0原文

我和我的朋友正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

奢华的一滴泪 2024-10-20 21:51:40

您的 $uptime 不是应在 date() 中使用的时间戳,而是时间差。你有一定的秒数,而不是时间戳(与实际日期相对应。

只需使用类似这样的东西来计算(快速计算,为 1 天、2 小时等添加一些额外的大脑);)

 $minutes = $uptime / 60;
 $hours   = $minuts/60 ;
 $days    = $hours / 24

等等

Your $uptime is not a timestamp as should be used in date(), 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) ;)

 $minutes = $uptime / 60;
 $hours   = $minuts/60 ;
 $days    = $hours / 24

etc

一个人的旅程 2024-10-20 21:51:40

如果您有 5.3 或更高版本,请使用 DateTime 和 DateInterval 类:

$uptimeStart = new DateTime(); //at the beginning of your script

function Uptime() {
  global $uptimeStart;
  $end = new DateTime();

  $diff = $uptimeStart->diff($end);

  return $diff->format("%a days %H:%i:%s");
}

If you have 5.3 or above, use the DateTime and DateInterval classes:

$uptimeStart = new DateTime(); //at the beginning of your script

function Uptime() {
  global $uptimeStart;
  $end = new DateTime();

  $diff = $uptimeStart->diff($end);

  return $diff->format("%a days %H:%i:%s");
}
美人如玉 2024-10-20 21:51:40

通过在该时差上调用 date() 不会得到任何有意义的信息。您应该将这个时间差逐渐除以年、月、日、小时,所有这些都以秒为单位。这样您就可以得到这些条件下的时差。

$daySeconds = 86400 ;
$monthSeconds = 86400 * 30 ;
$yearSeconds = 86400 * 365 ;

$years = $uptime / $yearSeconds ;
$yearsRemaining = $uptime % $yearSeconds ;

$months = $yearsRemaining / $monthSeconds ;
$monthsRemaining = $yearsRemaining % $monthSeconds ;

$days = $monthsRemaining / $daySeconds ;

..等等以获得小时和分钟。

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.

$daySeconds = 86400 ;
$monthSeconds = 86400 * 30 ;
$yearSeconds = 86400 * 365 ;

$years = $uptime / $yearSeconds ;
$yearsRemaining = $uptime % $yearSeconds ;

$months = $yearsRemaining / $monthSeconds ;
$monthsRemaining = $yearsRemaining % $monthSeconds ;

$days = $monthsRemaining / $daySeconds ;

.. etc to get hours and minutes.

顾北清歌寒 2024-10-20 21:51:40

第二个参数设置为 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文