繁体   English   中英

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

[英]PHP: date "Yesterday", "Today"

我有一点 function 显示最新活动,它从数据库中获取 unix 格式的时间戳,然后它与这一行相呼应:

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

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

我怎样才能做到这一点?

我会找到最后一个午夜的时间戳和它之前的时间戳,如果$last_access在两个时间戳之间,然后显示昨天,任何大于最后一个午夜的时间戳的都将是今天......

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

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

<?php
    if ($last_access >= strtotime("today"))
        echo "Today";
    else if ($last_access >= strtotime("yesterday"))
        echo "Yesterday";
?>
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);

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

如果我们是今天早上,那意味着昨天晚上就是今天(负 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');
    }
}

我增强了 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');
}

}

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

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

如果您按照上面的建议走这条路,今天/昨天有 unix 时间戳,请查看strtotime ,这是 20 世纪(或 21 世纪?)世纪最伟大的发明之一:

echo strtotime("yesterday"); // midnight
1281391200

echo strtotime("today"); // midnight
1281477600

echo strtotime("today, 1:30");
1281483000
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";
}

这是工作功能

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

希望对你有用...

$today=new DateTime('now');
$yesterday=date($today,strtotime("-1 day"));
date('d.m.Y',strtotime("-1 days"));

我们可以使用 DateTime class 的 diff 方法获得日期差异。

<?php
    $date1 = new DateTime("2022-07-26");
    $date2 = new DateTime("2022-07-27");
    
    $days = $date2->diff($date1)->format('%r%d');
    
    if($days === 0) {
        return 'Today';
    } else if($days === "-1") {
        return 'yesterday';
    }else {
        return $days . ' days later';
    }
?>

有关更多详细信息,请参阅

  1. 这个答案
  2. PHP 文档示例
  3. 如何计算两个日期之间的差异

波斯类型:

function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'سال',
        'm' => 'ماه',
        'w' => 'هفته',
        'd' => 'روز',
        'h' => 'ساعت',
        'i' => 'دقیقه',
        's' => 'ثانیه',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? ' روز' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' قبل' : 'اکنون';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM