【问题标题】:Check post "ago" time [duplicate]检查帖子“之前”时间[重复]
【发布时间】:2013-07-04 15:11:14
【问题描述】:

我正在尝试创建时间戳来检查帖子发布的“之前”时间,例如堆栈溢出,例如

现在

30 秒前

1 分钟前

1 小时前

等等。

1 小时前之后会显示正常的时间戳,例如:date("y/m/d - h/i");

我的问题是如何在帖子发布之前计算时间戳? (假设帖子是隐藏输入类型的一部分)

隐藏输入:

<input type="hidden" name="date" value="<?php echo date("l M Y - h-i") ?>"/>

编辑:

我的完整代码:

<?php
                            $listedTypes["doc"]             = 1;
                            $listedTypes["xlsx"]             = 2;
                            $listedTypes["txt"]             = 4;
                            $listedTypes["pdf"]             = 8;
                            $listedTypes["upload"]             = 16;
                            $listedTypes["all"]                = $listedTypes["pdf"] + $listedTypes["txt"] + $listedTypes["doc"] + $listedTypes["xlsx"] ;

                            if(!isset($_GET['doctype']))                        $_GET['doctype'] = "all";
                            if(!isset($listedTypes[$_GET['doctype']]))          $_GET['doctype'] = "all";
                            $requestedType = $listedTypes[$_GET['doctype']];
                            //Pages
                            $perpage = 10; // Avoid magic numbers
                            $files = glob('docs/*.xml');
                            $file_count = count($files);
                            $pages = ceil($file_count/$perpage);
                            $page = $_GET["page"];
                            $files = array_slice($files, ($page-1)*$perpage, $perpage);
                            if ((int) $page <= 0) { $page = 1; }
                            //Page - END
                            foreach ($files as $file){
                            $xml = new SimpleXMLElement($file, 0, true);


                            //Timestamp
                            $time = strtotime($xml->date);
                            function humanTiming ($time)
                            {

                                $time = time() - $time; // to get the time since that moment

                                $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':'');
                                }
                            }
                            //Timestamp - END

                            if($xml->doctype == "Microsoft Office Word")               $fileType = $listedTypes["doc"];
                            elseif($xml->doctype == "Microsoft Office Excel")        $fileType = $listedTypes["xlsx"];
                            elseif($xml->doctype == "Text File")        $fileType = $listedTypes["txt"];
                            elseif($xml->doctype == "Adobe PDF File")        $fileType = $listedTypes["pdf"];
                            elseif($xml->doctype == "upload")        $fileType = $listedTypes["upload"];
                            elseif($xml->doctype == "Adobe PDF File" || "Text File" || "Microsoft Office Word" || "Microsoft Office Excel")        $fileType = $listedTypes["all"];

                            if($fileType & $requestedType){
                                echo'
                                    <tr>
                                    <td>' . $xml->doctype . '</td>
                                    <td><a href="viewdoc.php?docname=' . basename($file, '.xml') . '&username='. $xml->startedby .'&myname='. $_SESSION['username'] .'">' . basename($file, '.xml') . '</a></td>
                                    <td><a href="viewprofile.php?name='. $xml->startedby .'">'. $xml->startedby .'</a></td>
                                    <td>'.humanTiming($time).' ago</td>
                                    <td>* * * * *</td>
                                    <td>'. filesize( $file ) .'kb</td>
                                    </tr>
                                    ';
                                }
                            }
                            ?>

前哨:

它只发布 1 个文件,但“之前”时间有效..

【问题讨论】:

    标签: php date timestamp html-parsing


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      使用此功能:

      function humanTiming ($time) {
          $time = time() - $time;
          if ($time > 3600) {
              return date("y/m/d h/i", $time);
          }
          else {
              $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':'');
              }
          }
      }
      

      $tokens 数组可以看出,一小时是3600,所以如果time() - $time 不止于此,那么您只需返回日期即可。

      【讨论】:

      • 是的,我做过类似的事情,但我的代码中的某些东西不能正常工作,我已经编辑了。
      • 尝试echo您的日期在各个时间点,以确保它以正确的格式发送。这是一个常见的错误。
      • 确实,谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      相关资源
      最近更新 更多