PHP: date "Yesterday", "Today"

Viewed 57702

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:

 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.

How can i do this?

12 Answers

I enhanced Thomas Decaux answer to come up with this

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...

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

we can get Date Difference using diff method of DateTime class.

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

for more details see

  1. this answer
  2. PHP doc example
  3. How to calculate the difference between two dates

Persian type:

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) . ' قبل' : 'اکنون';
}
Related