0

i wrote a simple method which from a date start it returns how many days hours and seconds were passed.

It returns for example 15000 days 3 hours 3 seconds ago ... seems not good to read at user side.

How can i do better?

For example somenthing like facebook posts time?

can anyone take care on this code?

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 and hope someone will enjoy this code

animuson
  • 53,861
  • 28
  • 137
  • 147
  • 2
    Don't try to reinvent the wheel (there are a ton of edge cases you're likely to miss if you try to). See [how to calculate the difference between two dates using PHP](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) here on SO. – Jonah Bishop Sep 20 '12 at 13:52
  • @JonahBishop that answer is very useful – Ghassan Elias Sep 20 '12 at 14:31

3 Answers3

1

Some time Before i wrote a Date function as

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);
    }   

use format_date("Any Date",10); to call

GajendraSinghParihar
  • 9,051
  • 11
  • 36
  • 64
0

I suppose you need it for a frontend, since you wrote about a human-readable date syntax.

I know that my solution is not in php, but i can suggest you this library:

http://momentjs.com/

it's a js toolset that could make you develop less code and mantain less software. it has the most common methods like add date, subtract date, etc.

In my experience it has been really helpful with dates handling

Stormsson
  • 1,391
  • 2
  • 16
  • 29
0

There was an issue on GitHub for the CodeIgniter 3 milestone, you can get many suggestions for your task there. There are a couple of variations available there: https://github.com/EllisLab/CodeIgniter/issues/309

The bonus is that it's going to be native to CodeIgniter

Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78