PHP:日期“昨天”,“今天”

问题描述 投票:13回答:9

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

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

现在我想将日期(j M -Y)替换为昨天,而今天如果最新活动在今天,则昨天也是如此。

我怎样才能做到这一点?

php datetime
9个回答
15
投票
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);

36
投票

如果$last_access介于两个时间戳之间,那么我会找到最后一个午夜的时间戳,如果<?php if ($last_access >= strtotime("today")) echo "Today"; else if ($last_access >= strtotime("yesterday")) echo "Yesterday"; ?> 介于两个时间戳之间,那么昨天显示的任何时间都比今天午夜的时间戳更大......

我相信这比做日期算术更快。

实际上,我刚刚测试了这段代码,它看起来效果很好:

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

8
投票

你必须每天比较,secondes comparaison是完全错误的:

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

这是我用于Kinoulink(一家法国创业公司)的方法:

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

3
投票

我加强了Thomas Decaux的答案来提出这个问题

strtotime

}

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


2
投票

如果您按照上面的建议继续前进,使用今天/昨天的unix时间戳,请查看echo strtotime("yesterday"); // midnight 1281391200 echo strtotime("today"); // midnight 1281477600 echo strtotime("today, 1:30"); 1281483000 ,这是20世纪(或21世纪)世纪最伟大的发明之一:

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";
}

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

0
投票

这是工作功能

$today=new DateTime('now');
$yesterday=date($today,strtotime("-1 day"));

希望为你工作......


0
投票
echo date("Y:m:d",strtotime("today"));
echo date("Y:m:d",strtotime("yesterday")); 

0
投票
qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.