【问题标题】:Days passed since post returns '44 years ago'自“44 年前”返回后已过去的天数
【发布时间】:2014-04-16 23:07:47
【问题描述】:

我试图找到解决方案,但没有成功。我有一个格式为 2014-04-03 19:21:30 的 $date 我尝试使用该函数回显自那天以来已经过去了多少天:

function timePassed($time){
        $time = time() - $time;
        $tokens = array (
            31536000 => 'year',
            2592000 => 'month',
            604800 => 'week',
            86400 => 'day',
            3600 => 'hour',
            60 => 'minute',
            1 => 'second'
        );

        foreach ($tokens as $unit => $text) {
            if ($time < $unit) continue;
            $numberOfUnits = floor($time / $unit);
            return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
        }
    }

我是 44 年前的……我做错了什么?

【问题讨论】:

标签: php


【解决方案1】:

这将使用您的逻辑为您提供经过的时间并返回 1 week 5 days 21 hours 4 minutes 36 seconds

使用 strtotime(),我将时间转换为相对于 1970 年 1 月 1 日的数字。您现在可以与 time() 进行比较。我还减去了每个步骤中经过的时间量,以便它可以继续找到较小的增量。

这段代码会产生:

1 week
1 year
20 minutes 38 seconds 

'

<?php
echo timePassed("2014-04-03 19:21:30") . "<br>";
echo timePassed("2013-02-03 19:21:30") . "<br>";
echo timePassed("2014-04-16 16:20:00") . "<br>";

function timePassed($time){
    $time = time() - strtotime($time);

    $tokens = array (
        31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
    );

    $return = "";
    foreach ($tokens as $unit => $text) {
        if ($time < $unit) continue;
        $numberOfUnits = floor($time / $unit);
        $time -= ($numberOfUnits * $unit);
        $return .= $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'') . " ";
        if ($unit > 60){
            // return if match greater than minutes
            return $return;
        }
    }
    return $return;
}

?>

要跟进您的上一个请求,以下代码应该足以让您根据需要对其进行修改。此代码将产生:

1 week
2013-02-03 19:21:30
23 hours 

.

<?php
echo timePassed("2014-04-03 19:21:30") . "<br>";
echo timePassed("2013-02-03 19:21:30") . "<br>";
echo timePassed("2014-04-16 16:20:00") . "<br>";


function timePassed($time){
    $origtime = $time;
    $time = time() - strtotime($time);
    $tokens = array (
        31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
    );
    $return = "";
    foreach ($tokens as $unit => $text) {
        if ($time < $unit) continue;
        $numberOfUnits = floor($time / $unit);
        $time -= ($numberOfUnits * $unit);
        $return .= $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'') . " ";
        if ($unit > 60){
            // return if match greater than hours
            if ($unit > 2592000 ) {
                // if units greater than one month, show the original time
                return $origtime;
            }
            elseif ($unit == 2592000  && $numberOfUnits > 1){
                // if units is months, show the original time if more than one month
                return $origtime;
            }
            else {
                // units greater than minutes, show the time without further detail
                return $return;
            }
        }
    }
    return $return;
}

?>

【讨论】:

  • 你应该指出 OP 的错误,而不是仅仅修复他们的代码。
  • 有没有办法整理回声做:如果刚刚发布:回声几秒前如果几分钟前:回声Xm Xs前如果豪斯前:回声X小时前如果几天前:回声X几天前,如果几周:回显 x 周前
  • 更新后的代码会如你所愿。请参阅包含的 3 个示例。
  • 对于第一个回声,我得到 1136289 1 周这是为什么?我认为与时间()有关,但不确定
  • 我不小心留下了一段调试代码。我已经删除了echo $time . "&lt;br&gt;";如果你删除这个,它会删除1 week之前的数字
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-20
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 2014-01-25
  • 2021-08-25
  • 1970-01-01
相关资源
最近更新 更多