2012-01-01 97 views
2

我想顯示「今天發佈」如果發佈是今天發佈,並且我知道如何執行該操作,但我希望顯示默認日期和時間格式截至上午12:01,顯然是因爲它不再發布「今天」,有沒有辦法我可以做到這一點?謝謝您的幫助。顯示相對時間午夜後更改爲默認日期


謝謝,我會盡力,這是我的。

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; } 
+4

顯示您已經嘗試過的內容或要添加此功能的位置的郵編... – 2012-01-01 18:30:07

回答

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"; 
} 
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> 

的功能要求,我想你datetime值從數據庫中獲取(針對帖子創建/發佈的日期),並輸出一個字符串。

如果我錯過了這一點,請讓我知道。