【问题标题】:Using the datetime format with PHP在 PHP 中使用日期时间格式
【发布时间】:2012-09-20 03:57:55
【问题描述】:

好的,所以我知道如何使用 PHP 格式化数据库中的“日期时间”值。现在,我想为帖子显示不同的时间,发布不同的时间。

例如,如果现在和发布日期之间的时间差是:

不到一个小时 - 显示分钟数前 多于一小时但少于 24 小时 - 显示几小时前 24 小时或更多但少于 48 小时 - 显示“昨天”和时间 48 小时或更长时间 - 显示日期、月份和时间

这是我目前所拥有的:

$date = date_create($row['date']); 
$HeaderDate = date_format(date_create($row['date']), 'F j, g:i a'); 
echo $HeaderDate;

这不测试任何条件,它只是回显月份、日期和时间,无论经过了多长时间。

现在,我只是不知道如何计算在我的 if 语句中使用的日期差异。我怎么做?但是在 PHP 中我根本不想使用 SQL,除了查询原始日期。

谢谢

【问题讨论】:

标签: php mysql


【解决方案1】:

我已使用此功能以漂亮的格式显示时间。希望这会对你有所帮助。

我认为您必须进行一些修改才能以您的方式获得它。正如您希望它显示两天后的实际时间。

<?php
function nicetime($date)
{
    if(empty($date)) {
        return "No date provided";
    }

    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");

    $now             = time();
    $unix_date         = strtotime($date);

       // check validity of date
    if(empty($unix_date)) {    
        return "Bad date";
    }

    // is it future date or past date
    if($now > $unix_date) {    
        $difference     = $now - $unix_date;
        $tense         = "ago";

    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }

    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }

    $difference = round($difference);

    if($difference != 1) {
        $periods[$j].= "s";
    }

    return "$difference $periods[$j] {$tense}";
}

$date = "2012-09-18 17:45";
$result = nicetime($date); // 2 days ago

?>

【讨论】:

    【解决方案2】:

    我将向您展示我使用的功能。您必须对其进行修改以获取所需的所有案例。你可以看到一个最小的例子here

    function timeToString($ptime) { // $ptime should be timestamp
        $etime = time() - $ptime;
    
        if ($etime <= 30)
            return 'few seconds ago';
    
        $a = array(
            10 * 12 * 30 * 24 * 60 * 60  =>  'decade',
            12 * 30 * 24 * 60 * 60       =>  'year',
            30 * 24 * 60 * 60            =>  'month',
            7 * 24 * 60 * 60             =>  'week',
            24 * 60 * 60                 =>  'day',
            60 * 60                      =>  'hour',
            60                           =>  'minute',
            1                            =>  'second'
             );
    
            foreach ($a as $secs => $str) {
                $d = $etime / $secs;
                if ($d >= 1) {
                    $r = floor($d);
                    if($etime > 7 * 24 * 60 * 60) {
                        $ret = 'on %1$s, %2$s%3$s';
                        return sprintf($ret,date('F', $ptime), date('d', $ptime),(date('Y') != date('Y', $ptime) ? ' ' . date('Y', $ptime) : ''));
                    }
                    $ret = '%d ' . $str . ($r > 1 ? 's' : '') . ' ago';
    
                    return sprintf($ret,$r);
            }
        }
    }
    

    【讨论】:

    • Is this your site??非常相似的功能...
    • 不,这不是我的网站.. 但这个功能是从那里得到灵感的 :)
    猜你喜欢
    • 1970-01-01
    • 2014-03-02
    • 2016-05-02
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多