0

我写了一个简单的方法,它从日期开始返回经过了多少天的小时和秒。

例如,它返回 15000 天 3 小时 3 秒前……在用户端似乎不太好阅读。

我怎样才能做得更好?

例如像facebook发布时间之类的东西?

任何人都可以照顾这段代码吗?

public function date_diff($start, $end="NOW")
    {
        $timeshift = false;
        $sdate = strtotime($start);
        $edate = strtotime($end);

        $time = $edate - $sdate;

        if($time >= 0 && $time <= 59) {
                // Seconds
              if($time == 1)
               {
                   $s = 'second';
               }
               else
               {
                   $s = 'seconds';
               }
                $timeshift = $time." ".$s;

        } elseif($time>=60 && $time<=3599) {
                // Minutes + Seconds
                $pmin = ($edate - $sdate) / 60;
                $premin = explode('.', $pmin);
                 if($premin[0] == 1)
               {
                   $m = 'min';
               }
               else
               {
                   $m = 'min';
               }

              //  $presec = $pmin-$premin[0];
                //$sec = $presec*60;

                $timeshift = $premin[0]." ".$m;//.round($sec,0); // sec ';

        } elseif($time>=3600 && $time<=86399) {
                // Hours + Minutes
                $phour = ($edate - $sdate) / 3600;
                $prehour = explode('.',$phour);

                $premin = $phour-$prehour[0];
                $min = explode('.',$premin*60);
                 if($prehour[0] > 1)
               {
                   $h = 'hours';
               }
               else
               {
                   $h = 'hour';
               }
                  if($min[0] == 1)
               {
                   $m = 'min';
               }
               else
               {
                   $m = 'min';
               }

           //     $presec = '0.'.$min[1];
            //    $sec = $presec*60;

                $timeshift = $prehour[0]." ".$h." ".$min[0]." ".$m;//.round($sec,0).' sec ';

        } elseif($time>=86400) {
                // Days + Hours + Minutes
                $pday = ($edate - $sdate) / 86400;
                $preday = explode('.',$pday);

                $phour = $pday-$preday[0];
                $prehour = explode('.',$phour*24);

                $premin = ($phour*24)-$prehour[0];
                $min = explode('.',$premin*60);

           //     $presec = '0.'.$min[1];
            //    $sec = $presec*60;


               if($preday[0] > 1)
               {
                   $d = 'days';
               }
               else
               {
                   $d = 'day';
               }
               if($prehour[0] > 1)
               {
                   $h = 'hours';
               }
               else
               {
                   $h = 'hour';
               }
               if($min[0] == 1)
               {
                   $m = 'min';
               }
               else
               {
                   $m = 'min';
               }

               $timeshift = $preday[0]." ".$d." ".$prehour[0]." ".$h." ".$min[0]." ".$m;

        }
        return $timeshift.' ago';
    }

thx 并希望有人会喜欢这段代码

4

3 回答 3

1

在我写一个日期函数之前的一段时间

function format_date($date, $type, $seperator="-")
    {
        if($date)
        {
            $day = date("j", strtotime($date));
            $month = date("n", strtotime($date));
            $year = date("Y", strtotime($date));
            $hour = date("H", strtotime($date));
            $min = date("i", strtotime($date));
            $sec = date("s", strtotime($date));

            switch($type)
            {
                case 0:  $date = date("Y".$seperator."m".$seperator."d",mktime($hour, $min, $sec, $month, $day, $year)); break;
                case 1:  $date = date("D, F j, Y",mktime($hour, $min, $sec, $month, $day, $year)); break;
                case 2:  $date = date("d".$seperator."m".$seperator."Y",mktime($hour, $min, $sec, $month, $day, $year)); break;
                case 3:  $date = date("d".$seperator."M".$seperator."Y",mktime($hour, $min, $sec, $month, $day, $year)); break;
                case 4:  $date = date("d".$seperator."M".$seperator."Y h:i A",mktime($hour, $min, $sec, $month, $day, $year)); break;
                case 5:  $date = date("m".$seperator."d".$seperator."Y",mktime($hour, $min, $sec, $month, $day, $year)); break;
                case 6:  $date = date("M",mktime($hour, $min, $sec, $month, $day, $year)); break;
                case 7:  $date = date("Y",mktime($hour, $min, $sec, $month, $day, $year)); break;
                case 8:  $date = date("j",mktime($hour, $min, $sec, $month, $day, $year)); break;
                case 9:  $date = date("n",mktime($hour, $min, $sec, $month, $day, $year)); break;
                case 10: 
                                 $diff = abs(strtotime($date) - strtotime(date("Y-m-d h:i:s"))); 
                                 $years = floor($diff / (365*60*60*24));
                                 $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
                                 $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
                                $date = $years ." years, ".$months. " months, ". $days.  "days";
            }
        }
        return($date);
    }   

用来format_date("Any Date",10);打电话

于 2012-09-20T14:06:29.523 回答
0

我想你需要它作为前端,因为你写了一个人类可读的日期语法。

我知道我的解决方案不在 php 中,但我可以向您推荐这个库:

http://momentjs.com/

它是一个 js 工具集,可以让您开发更少的代码并维护更少的软件。它有最常用的方法,如添加日期、减去日期等。

根据我的经验,它对日期处理非常有帮助

于 2012-09-20T13:48:54.783 回答
0

CodeIgniter 3 里程碑的 GitHub 上有一个问题,您可以在那里获得许多关于您的任务的建议。那里有几个变体:https ://github.com/EllisLab/CodeIgniter/issues/309

好处是它将原生于 CodeIgniter

于 2012-09-20T13:51:05.180 回答