0

I'm trying to create a PHP function where I enter a $date variable with this format: 03/09/2016 - 12:02.

I created a function to turn this date / time variable into a string that says the $date variable was x days and hours ago.

Function:

$date = $r->date;

                function nicetime($date)
                {
                    if(empty($date)) {
                        return "Geen datum gevonden.";
                    }

                    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
                    $lengths         = array("60","60","24","7","4.35","12","10");

                    ini_set('date.timezone', 'Europe/Berlin');
                    $now             = time('Y-m-d H:i:s');
                    $unix_date       = $date;

                       // check validity of date
                    if(empty($unix_date)) {    
                        return "Error.";
                    }

                    // is it future date or past date
                    if($now > $unix_date) {    
                        $difference     = $now - $unix_date;
                        $tense         = "ago";

                    } else {
                        $difference     = $unix_date - $now;
                        $tense         = "from now";
                    }

                    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
                        $difference /= $lengths[$j];
                    }

                    $difference = round($difference);

                    if($difference != 1) {
                        $periods[$j].= "s";
                    }

                    return "$difference $periods[$j] {$tense}";
                }

                $postdate = nicetime($date);

So I use $date to insert the date. And the outcome of $postdate is 47 years ago. Does someone know what I'm doing wrong? Because I shouldn't get 47 years ago.

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
Jesse
  • 15
  • 3
  • first I wonder why you don't use built in DateTime-Class, including DateInterval. Would be much easier to accomplish this. – Jeff Sep 03 '16 at 13:06
  • Because I dont know how to use that – Jesse Sep 03 '16 at 13:07
  • second: what is $date? A string containing "03/09/2016 - 12:02"? Or a timestamp? – Jeff Sep 03 '16 at 13:08
  • $date is a string containing "03/09/2016 - 12:02" – Jesse Sep 03 '16 at 14:04
  • Possible duplicate of [Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...](http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago) – Qirel Sep 03 '16 at 16:21

1 Answers1

1

So lets split your job to 3 steps:

Step 1: fork the time and the date. and then get then get the long time using the strtotime() function

Step 2: get the current long time from the time() function

Step 3: Now - Unix date

<?php
    $date = $r->date;

        function nicetime($date)
        {
            if(empty($date)) {
                return "Geen datum gevonden.";
            }
            $r1 = split('/', $date);
            $day = $r1[0]; 
            $month = $r1[1]; 
            $year = split(' ', $r1[2]);
            $year = $year[0];
            $r1 = split('-', $date);

            $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
            $lengths         = array("60","60","24","7","4.35","12","10");

            ini_set('date.timezone', 'Europe/Berlin');

            $now             = time();
            $unix_date       = strtotime($month.'/'.$day.'/'.$year.' '. $r1[1])."</br>";;

               // check validity of date
            if(empty($unix_date)) {    
                return "Error.";
            }

            // is it future date or past date
            if($now > $unix_date) {    
                $difference     = $now - $unix_date;
                $tense         = "ago";

            } else {
                $difference     = $unix_date - $now;
                $tense         = "from now";
            }

            for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
                $difference /= $lengths[$j];
            }

            $difference = round($difference);

            if($difference != 1) {
                $periods[$j].= "s";
            }

            return "$difference $periods[$j] {$tense}";
        }

        $postdate = nicetime($date); // 2 days ago
        echo $postdate;
?>

Incidentally, You did a nice work.

Update: While using loop to this function make sure to enclose your function in a conditional block:

   if (!function_exists('nicetime')) {
    // ... proceed to declare your function
}
  • Thanks a lot! Its working now, but I am using this for Forum Posts to show the time its posted, so I'm using it in a While loop, but now it shows the time of the latest post, because it can't redeclare the function. Do you have a way to fix this? – Jesse Sep 03 '16 at 14:03