2010-08-10 198 views
12

我有一個小功能,顯示最新的活動,它抓住時間戳從數據庫UNIX格式,然後將其與該行回聲出:PHP:日期「昨天」,「今天」

date("G:i:s j M -Y", $last_access) 

現在我想將日期(j M -Y)替換爲昨天,而今天如果最近的活動在今天之內,並且與昨天相同。

我該怎麼做?

+0

到目前爲止,您是否有任何代碼? – 2010-08-10 23:29:30

+0

你可以請嘗試更精確?我不明白昨天和今天你的意思......坦率地說;-) – maraspin 2010-08-10 23:36:39

+2

明天你需要擔心,而不是昨天或今天,清除所有這些蜘蛛網和悲傷,只知道這是一天的時間,所有那。 – 2010-08-10 23:42:13

回答

14
function get_day_name($timestamp) { 

    $date = date('d/m/Y', $timestamp); 

    if($date == date('d/m/Y')) { 
     $date = 'Today'; 
    } 
    else if($date == date('d/m/Y',now() - (24 * 60 * 60))) { 
     $date = 'Yesterday'; 
    } 
    return $date; 
} 
print date('G:i:s', $last_access).' '.get_day_name($last_access); 
+0

我得到錯誤意外'{' – Karem 2010-08-11 00:06:57

+1

'else if'行似乎缺少一個')'。 – Maerlyn 2010-08-11 00:25:55

+0

對不起,爲你解決了Karem。我猜想它適合你。 – Keyo 2010-08-11 03:09:28

35

我會找到最後午夜timestap和其前一個,如果$last_access是兩個時間戳之間,然後顯示昨日,任何比去年午夜的時間戳更大的將是今天 ...

我相信這會比做日期算法更快。

其實,我只是測試此代碼,它似乎偉大的工作:

<?php 
    if ($last_access >= strtotime("today")) 
     echo "Today"; 
    else if ($last_access >= strtotime("yesterday")) 
     echo "Yesterday"; 
?> 
+0

比我的回答更乾淨+1 – Keyo 2010-10-14 23:54:21

+0

+1 This Works great,thanks! :) – Nathan 2011-12-28 20:36:04

1
something like: 

$now = time(); 

$last_midnight = $now - ($now % (24*60*60)); 

if ($last_access >= $last_midnight) 
{ 
print "Today"; 
}  
elseif ($last_access >= ($last_midnight-(24*60*60)) 
{ 
Print "Yesterday"; 
} 
+0

@mvds:我不知道你可以在strtotime中使用相對字符串。尼斯。儘管PHP4 vs PHP5有些問題。 – tcrosley 2010-08-11 00:17:58

+0

我實際上在一些基於網絡的搜索服務中使用它,以允許用戶輸入滑動窗口:例如,從「兩個月前」到「今天」。 – mvds 2010-08-11 00:25:51

2

如果你想在路上如上建議,對Unix時間戳今天/昨天,看看在strtotime,20最偉大的發明之一世紀(或21):

echo strtotime("yesterday"); // midnight 
1281391200 

echo strtotime("today"); // midnight 
1281477600 

echo strtotime("today, 1:30"); 
1281483000 
7

你必須到c有一天ompare日,secondes comparaison是完全錯誤的:

如果我們今天早上,這意味着今天昨晚是(通過減去24小時)^^

這裏我用Kinoulink(法國啓動)的方法:

public function formatDateAgo($value) 
{ 
    $time = strtotime($value); 
    $d = new \DateTime($value); 

    $weekDays = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi', 'Dimanche']; 
    $months = ['Janvier', 'Février', 'Mars', 'Avril',' Mai', 'Juin', 'Juillet', 'Aout', 'Septembre', 'Octobre', 'Novembre', 'Décembre']; 

    if ($time > strtotime('-2 minutes')) 
    { 
     return 'Il y a quelques secondes'; 
    } 
    elseif ($time > strtotime('-30 minutes')) 
    { 
     return 'Il y a ' . floor((strtotime('now') - $time)/60) . ' min'; 
    } 
    elseif ($time > strtotime('today')) 
    { 
     return $d->format('G:i'); 
    } 
    elseif ($time > strtotime('yesterday')) 
    { 
     return 'Hier, ' . $d->format('G:i'); 
    } 
    elseif ($time > strtotime('this week')) 
    { 
     return $weekDays[$d->format('N') - 1] . ', ' . $d->format('G:i'); 
    } 
    else 
    { 
     return $d->format('j') . ' ' . $months[$d->format('n') - 1] . ', ' . $d->format('G:i'); 
    } 
} 
+0

太棒了!這只是錯過了一年,在最後一個「其他」。但這是一種美。謝謝! – 2016-10-04 12:53:21

0

這裏的工作職能

function minus_one_day($date){ 
     $date2 = formatDate4db($date); 
     $date1 = str_replace('-', '/', $date2); 
     $yesterday = date('Y-m-d',strtotime($date1 . "-1 days")); 
     return $yesterday; } 

你希望工作...

0

我增強托馬斯德科答案想出這個

function formatTimeString($timeStamp) { 
$str_time = date("Y-m-d H:i:sP", $timeStamp); 
$time = strtotime($str_time); 
$d = new DateTime($str_time); 

$weekDays = ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']; 
$months = ['Jan', 'Feb', 'Mar', 'Apr', ' May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']; 

if ($time > strtotime('-2 minutes')) { 
    return 'Just now'; 
} elseif ($time > strtotime('-59 minutes')) { 
    $min_diff = floor((strtotime('now') - $time)/60); 
    return $min_diff . ' min' . (($min_diff != 1) ? "s" : "") . ' ago'; 
} elseif ($time > strtotime('-23 hours')) { 
    $hour_diff = floor((strtotime('now') - $time)/(60 * 60)); 
    return $hour_diff . ' hour' . (($hour_diff != 1) ? "s" : "") . ' ago'; 
} elseif ($time > strtotime('today')) { 
    return $d->format('G:i'); 
} elseif ($time > strtotime('yesterday')) { 
    return 'Yesterday at ' . $d->format('G:i'); 
} elseif ($time > strtotime('this week')) { 
    return $weekDays[$d->format('N') - 1] . ' at ' . $d->format('G:i'); 
} else { 
    return $d->format('j') . ' ' . $months[$d->format('n') - 1] . 
    (($d->format('Y') != date("Y")) ? $d->format(' Y') : "") . 
    ' at ' . $d->format('G:i'); 
} 

}

它發生在時間戳作爲參數,它增加了時間的一年,如果它是從不同的年份,等...