我有一个小功能,显示最新的活动,它抓住时间戳从数据库UNIX格式,然后将其与该行回声出:PHP:日期“昨天”,“今天”
date("G:i:s j M -Y", $last_access)
现在我想将日期(j M -Y)替换为昨天,而今天如果最近的活动在今天之内,并且与昨天相同。
我该怎么做?
我有一个小功能,显示最新的活动,它抓住时间戳从数据库UNIX格式,然后将其与该行回声出:PHP:日期“昨天”,“今天”
date("G:i:s j M -Y", $last_access)
现在我想将日期(j M -Y)替换为昨天,而今天如果最近的活动在今天之内,并且与昨天相同。
我该怎么做?
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);
我会找到最后午夜timestap和其前一个,如果$last_access
是两个时间戳之间,然后显示昨日,任何比去年午夜的时间戳更大的将是今天 ...
我相信这会比做日期算法更快。
其实,我只是测试此代码,它似乎伟大的工作:
<?php
if ($last_access >= strtotime("today"))
echo "Today";
else if ($last_access >= strtotime("yesterday"))
echo "Yesterday";
?>
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";
}
如果你想在路上如上建议,对Unix时间戳今天/昨天,看看在strtotime
,20最伟大的发明之一世纪(或21):
echo strtotime("yesterday"); // midnight
1281391200
echo strtotime("today"); // midnight
1281477600
echo strtotime("today, 1:30");
1281483000
你必须到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');
}
}
太棒了!这只是错过了一年,在最后一个“其他”。但这是一种美。谢谢! – 2016-10-04 12:53:21
这里的工作职能
function minus_one_day($date){
$date2 = formatDate4db($date);
$date1 = str_replace('-', '/', $date2);
$yesterday = date('Y-m-d',strtotime($date1 . "-1 days"));
return $yesterday; }
你希望工作...
我增强托马斯德科答案想出这个
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');
}
}
它发生在时间戳作为参数,它增加了时间的一年,如果它是从不同的年份,等...
到目前为止,您是否有任何代码? – 2010-08-10 23:29:30
你可以请尝试更精确?我不明白昨天和今天你的意思......坦率地说;-) – maraspin 2010-08-10 23:36:39
明天你需要担心,而不是昨天或今天,清除所有这些蜘蛛网和悲伤,只知道这是一天的时间,所有那。 – 2010-08-10 23:42:13