如何打印日期,例如:2 小时前,mar 10

发布于 2024-11-07 13:57:42 字数 1611 浏览 2 评论 0原文

  function timeSpent($date_created, $endingDay)
  {
      $diff = abs(strtotime($date_created) - strtotime(date("Y-m-d H:i:s")));
      $years = floor($diff / (365 * 60 * 60 * 24));
      $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
      $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
      $hours = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24) / (60 * 60));
      $minuts = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60) / 60);
      $seconds = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60 - $minuts * 60));
           $week = floor($days / 7);
          $day = $days % 7;
          if ($week < 1 && $week != 0) {
             return $days . " days - " . $hours . " hrs - " . $minuts . "- min" . $week;
          } elseif ($week == 0) {
              if ($days != 0) {
                  return $days . "days " . $hours . "hours ago";
              } elseif ($hours != 0) {
                  return $hours . "hours " . $minuts . "minute ago";
              } elseif ($minuts) {
                  return $minuts . "minute " . $seconds . "seconds ago";
              } else {
                  return "few seconds ago";
              }
          } else {
              $stamp = strtotime($date_created);
              return date("F d Y", $stamp);
          }
      }
  }

我正在使用这个功能,它无法正常工作。

  function timeSpent($date_created, $endingDay)
  {
      $diff = abs(strtotime($date_created) - strtotime(date("Y-m-d H:i:s")));
      $years = floor($diff / (365 * 60 * 60 * 24));
      $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
      $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
      $hours = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24) / (60 * 60));
      $minuts = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60) / 60);
      $seconds = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60 - $minuts * 60));
           $week = floor($days / 7);
          $day = $days % 7;
          if ($week < 1 && $week != 0) {
             return $days . " days - " . $hours . " hrs - " . $minuts . "- min" . $week;
          } elseif ($week == 0) {
              if ($days != 0) {
                  return $days . "days " . $hours . "hours ago";
              } elseif ($hours != 0) {
                  return $hours . "hours " . $minuts . "minute ago";
              } elseif ($minuts) {
                  return $minuts . "minute " . $seconds . "seconds ago";
              } else {
                  return "few seconds ago";
              }
          } else {
              $stamp = strtotime($date_created);
              return date("F d Y", $stamp);
          }
      }
  }

i am using this function it doesn't work properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

回眸一遍 2024-11-14 13:57:42

我注意到的一个错误是,如果您输入 4 月 15 日,则可能有 1 个月零 0 周。这将导致仅输出日期和小时。更改以下内容。

} elseif ($week == 0) {

} elseif ($week == 0 && $months == 0 && $years == 0) {

根据您输入的日期,这可能会导致您的问题

One error I notice is that it can be possible to have 1 month and 0 weeks like if you were to enter April 15th. Which would result in only outputting the days and hours. Change the following.

} elseif ($week == 0) {

to

} elseif ($week == 0 && $months == 0 && $years == 0) {

This could be causing your problem depending on the date you entered.

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