在PHP中使用datetime格式

在PHP中使用datetime格式,php,mysql,Php,Mysql,好的,我知道如何用PHP格式化数据库中的“datetime”值。现在,我想用不同的时间来显示帖子的时间,发布时间不同 例如,如果现在与发布日期之间的时差为: 不到一小时-显示分钟数 超过小时但少于24小时-显示小时数 24小时或以上但少于48小时-显示“昨天”和时间 48小时或以上-显示日期、月份和时间 以下是我到目前为止的情况: $date = date_create($row['date']); $HeaderDate = date_format(date_create($row['dat

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

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

不到一小时-显示分钟数 超过小时但少于24小时-显示小时数 24小时或以上但少于48小时-显示“昨天”和时间 48小时或以上-显示日期、月份和时间

以下是我到目前为止的情况:

$date = date_create($row['date']); 
$HeaderDate = date_format(date_create($row['date']), 'F j, g:i a'); 
echo $HeaderDate;
这并没有测试任何条件,它只是echo的月份,日期和时间,无论持续时间已经过去

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


谢谢

我用这个函数以很好的格式显示时间。希望这对你有帮助

我想你得修改一下,以使它碍事。因为你想让它显示两天后的实际时间

<?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

?>

我用这个函数以很好的格式显示时间。希望这对你有帮助

我想你得修改一下,以使它碍事。因为你想让它显示两天后的实际时间

<?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

?>

我将向您展示我使用的函数。您必须对其进行修改,以获得所需的所有案例。您可以看到一个最小的示例

函数timeToString($ptime){/$ptime应该是timestamp
$etime=time()-$ptime;
如果($etime‘十年’,
12*30*24*60*60=>年份,
30*24*60*60=>“月”,
7*24*60*60=>“周”,
24*60*60=>“天”,
60*60=>“小时”,
60=>“分钟”,
1=>“秒”
);
foreach($a作为$secs=>$str){
$d=$etime/$secs;
如果($d>=1){
$r=底价($d);
如果($etime>7*24*60*60){
$ret='在%1$s、%2$s%3$s'上;
返回sprintf($ret,date('F',$ptime),date('d',$ptime),(date('Y')!=date('Y',$ptime)?“”。date('Y',$ptime):“”);
}
$ret='%d'。$str。($r>1?'s':'')。'ago';
返回sprintf($ret,$r);
}
}
}

我将向您展示我使用的函数。您必须对其进行修改,以获得所需的所有案例。您可以看到一个最小的示例

函数timeToString($ptime){/$ptime应该是timestamp
$etime=time()-$ptime;
如果($etime‘十年’,
12*30*24*60*60=>年份,
30*24*60*60=>“月”,
7*24*60*60=>“周”,
24*60*60=>“天”,
60*60=>“小时”,
60=>“分钟”,
1=>“秒”
);
foreach($a作为$secs=>$str){
$d=$etime/$secs;
如果($d>=1){
$r=底价($d);
如果($etime>7*24*60*60){
$ret='在%1$s、%2$s%3$s'上;
返回sprintf($ret,date('F',$ptime),date('d',$ptime),(date('Y')!=date('Y',$ptime)?“”。date('Y',$ptime):“”);
}
$ret='%d'。$str。($r>1?'s':'')。'ago';
返回sprintf($ret,$r);
}
}
}

你需要的是使用,还有。比我所知道的更多的副本。。。你需要的是使用,还有。比我知道的更多的副本?非常相似的功能…不,这不是我的网站。。但是这个功能是从那里得到启发的:)?非常相似的功能…不,这不是我的网站。。但此功能的灵感来自于此:)