PHP: date “Yesterday”, “Today”

后端 未结 9 1762
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 10:20

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\         


        
相关标签:
9条回答
  • 2020-12-09 11:03

    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) ^^

    Here a method I use for Kinoulink ( a french startup ) :

    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');
        }
    }
    
    0 讨论(0)
  • 2020-12-09 11:04
    echo date("Y:m:d",strtotime("today"));
    echo date("Y:m:d",strtotime("yesterday")); 
    
    0 讨论(0)
  • 2020-12-09 11:07
    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);
    
    0 讨论(0)
  • 2020-12-09 11:07
    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";
    }
    
    0 讨论(0)
  • 2020-12-09 11:08

    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:

    echo strtotime("yesterday"); // midnight
    1281391200
    
    echo strtotime("today"); // midnight
    1281477600
    
    echo strtotime("today, 1:30");
    1281483000
    
    0 讨论(0)
  • 2020-12-09 11:09
    $today=new DateTime('now');
    $yesterday=date($today,strtotime("-1 day"));
    
    0 讨论(0)
提交回复
热议问题