2010-08-10 202 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'); 
} 

}

它发生在时间戳作为参数,它增加了时间的一年,如果它是从不同的年份,等...