PHP:日期“昨天”,“今天”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3454258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: date "Yesterday", "Today"
提问by Karem
I have a little function that shows latest activity, it grab timestamp in unix format from the db, and then it echo out with this line:
我有一个显示最新活动的小函数,它从数据库中以 unix 格式获取时间戳,然后用以下行回显:
date("G:i:s j M -Y", $last_access)
Now i would like to replace the date (j M -Y) to Yesterday, and Today if the latest activity was within today, and same goes with Yesterday.
现在我想将日期(j M -Y)替换为昨天和今天,如果最新的活动在今天之内,昨天也是如此。
How can i do this?
我怎样才能做到这一点?
采纳答案by Keyo
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);
回答by Hameed
I would find the timestap for last midnight and the one before it, if $last_access
is between the two timestamps, then display yesterday, anything greater than last midnight's timestamp would be today...
我会找到最后一个午夜的时间戳和它之前的一个,如果$last_access
在两个时间戳之间,然后显示昨天,任何大于最后一个午夜的时间戳的都是今天......
I believe that would be the quicker than doing date arithmetic.
我相信这会比做日期算术更快。
Actually, I just tested this code and it seems to work great:
实际上,我刚刚测试了这段代码,它似乎工作得很好:
<?php
if ($last_access >= strtotime("today"))
echo "Today";
else if ($last_access >= strtotime("yesterday"))
echo "Yesterday";
?>
回答by Thomas Decaux
You have to compare day with day, secondes comparaison are totally wrong :
你必须日复一日地比较,秒比较是完全错误的:
If we are today morning, that means yesterday night is today (by minus 24h) ^^
如果我们今天早上,那意味着昨天晚上是今天(负24小时)^^
Here a method I use for Kinoulink ( a french startup ) :
这是我用于 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');
}
}
回答by Joshua
I enhanced Thomas Decaux answer to come up with this
我增强了 Thomas Decaux 的回答以提出这个
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');
}
}
}
It takes in the time stamp as the argument, it adds the year of the time if it was from a different year, etc...
它以时间戳作为参数,如果它来自不同的年份,它会添加时间的年份,等等......
回答by mvds
If you are going down the road as suggested above, with unix timestamps for today / yesterday, have a look at strtotime
, one of the greatest inventions of the 20th (or 21st?) century:
如果您按照上面的建议走下去,使用今天/昨天的 unix 时间戳,看看strtotime
20 世纪(或 21 世纪?)最伟大的发明之一:
echo strtotime("yesterday"); // midnight
1281391200
echo strtotime("today"); // midnight
1281477600
echo strtotime("today, 1:30");
1281483000
回答by d?lo sürücü
echo date("Y:m:d",strtotime("today"));
echo date("Y:m:d",strtotime("yesterday"));
回答by tcrosley
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";
}
回答by d?lo sürücü
$today=new DateTime('now');
$yesterday=date($today,strtotime("-1 day"));
回答by Muhammad Awais Zulifqar
here's working function
这是工作功能
function minus_one_day($date){
$date2 = formatDate4db($date);
$date1 = str_replace('-', '/', $date2);
$yesterday = date('Y-m-d',strtotime($date1 . "-1 days"));
return $yesterday; }
hope work for you...
希望对你有用...