PHP:date";“昨天”&引用;“今天”;

PHP:date";“昨天”&引用;“今天”;,php,datetime,Php,Datetime,我有一个显示最新活动的小函数,它从db中获取unix格式的时间戳,然后用以下行进行响应: date("G:i:s j M -Y", $last_access) 现在,我想将日期(j M-Y)替换为昨天,如果最近的活动在今天之内,则替换为今天,昨天也是如此 我该怎么做呢?我会找到最后一个午夜的时间点和它之前的时间点,如果$last\u access在两个时间戳之间,那么显示昨天,任何大于最后一个午夜的时间戳都将是今天 我相信这比做日期算术要快 实际上,我刚刚测试了这段代码,它似乎工作得很好:

我有一个显示最新活动的小函数,它从db中获取unix格式的时间戳,然后用以下行进行响应:

 date("G:i:s j M -Y", $last_access)
现在,我想将日期(j M-Y)替换为昨天,如果最近的活动在今天之内,则替换为今天,昨天也是如此


我该怎么做呢?

我会找到最后一个午夜的时间点和它之前的时间点,如果
$last\u access
在两个时间戳之间,那么显示昨天,任何大于最后一个午夜的时间戳都将是今天

我相信这比做日期算术要快

实际上,我刚刚测试了这段代码,它似乎工作得很好:

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

如果您要按照上面的建议走下去,今天/昨天都有unix时间戳,那么请看一下20世纪(或21世纪?)最伟大的发明之一&
strotime


你必须日复一日地比较,秒的比较是完全错误的:

如果我们是今天早上,那意味着昨天晚上就是今天(减24小时)^^

这里是我对Kinoulink(一家法国初创公司)使用的一种方法:


这是工作函数

    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');
}
}


它以时间戳作为参数,如果它来自不同的年份,它会添加年份,等等。

到目前为止,您是否已经编写了任何代码?请尝试更精确一些?我不明白你说的昨天和今天是什么意思。。。坦率地说;-)你需要担心的是明天,而不是昨天或今天,清除所有的蜘蛛网和悲伤,只知道还有一天就可以了。@mvds:我不知道你可以在strottime中使用相对字符串。美好的不过有些问题是关于PHP4和PHP5的。实际上,我在一些基于web的搜索服务中使用它,允许用户输入一个滑动窗口:例如从“两个月前”到“今天”。如果
行似乎缺少
,则
。抱歉,为您解决了这个问题。我想这对你来说是可行的。函数now()不存在。使用time()一次获取离现在的天数可能更容易,因此相同的值可以使用两次?比我的答案干净多了+1太棒了!它只是错过了去年,在最后的《else》。但这是一种美。谢谢很不错的。非常感谢。我很乐意帮忙,你能进一步解释一下吗?大多数其他答案需要某种比较才能打印昨天的<代码>或今天的<代码>但是您的代码缺少它-您确定这解决了给定的问题吗?$Today=new DateTime('now')$昨天=日期($今天,标准时间(“-1天”);也许不是炒菜……这看起来和你在答案中发布的代码一样。但是它怎么打印东西呢?它是如何打印昨天的
或今天的
字符串的?您好!虽然这可能是问题的解决方案,但通常不鼓励只使用代码的答案。请花一些时间编辑您的回复,并解释为什么这是一个解决方案,因为它将帮助OP和网站的未来访问者。
$today=new DateTime('now');
$yesterday=date($today,strtotime("-1 day"));
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');
    }
}
    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');
}
$today=new DateTime('now');
$yesterday=date($today,strtotime("-1 day"));
echo date("Y:m:d",strtotime("today"));
echo date("Y:m:d",strtotime("yesterday"));