-3

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

现在

30 秒前

1 分钟前

1小时前

ETC..

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 个文件,但“以前”时间有效..

4

2 回答 2

1

这个网站上已经有一些很棒的功能应该会有所帮助:

于 2013-07-06T21:03:55.723 回答
0

使用这个功能:

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超过了,那么您只需要返回日期即可。

于 2013-07-06T21:13:38.157 回答