如何查找自日期时间以来经过的时间以及通知facebook的时间?

时间:2016-06-29 07:48:20

标签: php

我的代码是这样的:

<?php
    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' => 'year',
            'm' => 'month',
            'w' => 'week',
            'd' => 'day',
            'h' => 'hour',
            'i' => 'minute',
            's' => 'second',
        );
        foreach ($string as $k => &$v) {
            if ($diff->$k) {
                $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
            } else {
                unset($string[$k]);
            }
        }

        if (!$full) $string = array_slice($string, 0, 1);
        return $string ? implode(', ', $string) . ' ago' : 'just now';
    }

    $created_date = '2016-06-28 23:30:00';

    echo time_elapsed_string($created_date);
?>

我想在通知facebook中制作日期时间

就像这样:

6月26日11:45

昨天上午7点

12分钟前

我想这样做

解决我问题的任何解决方案?

1 个答案:

答案 0 :(得分:1)

我已更新你的功能。请添加所需的验证

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' => 'year',
            'm' => 'month',
            'w' => 'week',
            'd' => 'day',
            'h' => 'hour',
            'i' => 'minute',
            's' => 'second',
        );
        foreach ($string as $k => &$v) {
            if ($diff->$k) {
                $v = $diff->$k ;//. ' ' . $v . ($diff->$k > 1 ? 's' : '');

            } else {
                unset($string[$k]);
            }
        }

        if (!$full) $string = array_slice($string, 0, 1);

     if(!empty($string['y']) || !empty($string['m']) || !empty($string['w']) )
        {
            echo showdate($datetime,'w')."<br>";
        } 
     elseif(!empty($string['d']) )
        {
            if($string['d'] > 1)
            {
              echo showdate($datetime,'w')."<br>";
            }
            else
            {
               echo showdate($datetime,'d')."<br>";
            }
        }
     elseif(!empty($string['h']) )
        {
         echo implode(', ', $string) . 'h ago';//"<br>";
        }
        elseif(!empty($string['i'])  )
        {
          echo implode(', ', $string) . 'mints ago';//"<br>";
        }
        elseif( !empty($string['s']))
       {
         echo 'just now';//"<br>";
        }
     // return $string ? implode(', ', $string) . ' ago' : 'just now';
    }


function showdate($dt,$type)
{
    $mydt  = new DateTime($dt);
    if($type == 'd')
    {
        $str = 'Yesterday at '.$mydt->format('H:i:s ');
    }
    elseif($type == 'h')
    {
        //$str = 'Yesterday at '.$mydt->format('H:i:s ');
    }
    else
    {
        $str = $mydt->format('M d');
        $str .= ' at '.$mydt->format('H:i:s ');
    }

    return $str;
}
    $created_date = '2016-06-29 04:50:00';

    echo time_elapsed_string($created_date);