显示午夜后更改为默认日期的相对时间

时间:2012-01-01 18:28:30

标签: php

我想显示“今天发布”,如果今天发布的帖子,我知道怎么做,但我希望它显示截至上午12:01的默认日期和时间格式,显然是因为ita不再发布“今天”,有没有办法可以做到这一点?谢谢你的帮助。


谢谢,我会试试,这就是我所拥有的。

if($params['time'] > (time() - (60*60*24))){

$old_time = $params['time'];

$hm = date("g:ia", $old_time);

$today = elgg_echo('friendly_time_today', array($hm));

return $today;

    return $today;

  } else if($params['time'] > (time() - (60*60*48))){

$old_time = $params['time'];

$hm = date("g:ia", $old_time);

$yesturday = elgg_echo('friendly_time_yesturday', array($hm));

return $yesturday;

    return $yesturday; }

2 个答案:

答案 0 :(得分:0)

你的意思是这样的:

<?php
$sSaved = "11:08am 01.01.2012"; // comes from date("H:ia d.m.Y");
$aSaved = explode(" ", $sSaved);
if ($aSaved[1] != date("d.m.Y")) {
    echo $sSaved;
} else {
    echo "today";
} 

答案 1 :(得分:0)

如果我理解你的帖子,这可能会以某种方式理解:

# example function
function stylePostDateExample (DateTime $postDate) {
    # get current date
    $currDate = date_create(date('Y-m-d H:i:s'));
    # grab the interval between the post date and current date
    $intervalObj = date_diff($postDate, $currDate);

    # simple output start
    $stringOut = "Posted ";

    # interpret difference
    if ($intervalObj->format('%d') < 1 && $intervalObj->format('%y%m') == 0){
        # still within the day
        $stringOut .= "today at " . $intervalObj->format('%H:%I');
    } else {
        # the post date day has passed
        $stringOut .= "on " . date_format($postDate, 'Y-m-d H:i:s');
    }
    return $stringOut;
}

# test: any previous day
$postDate = date_create('2012-01-01 00:00:00');
echo stylePostDateExample ($postDate);

# results
# -----------------------------------------
# Posted on <date value here>

# test: today
$postDate = date_create(date('Y-m-d'));
echo stylePostDateExample ($postDate);

# results
# -----------------------------------------
# Posted today at <time value here>

该函数需要一个日期时间值,我想你是从数据库中获取的(对于帖子的创建/发布日期),它会输出一个字符串。

如果我错过了这一点,请告诉我。