0

The output of the below code is 2 Weeks, 1 Day . Whereas i want to print in number of days like 15 days and so on. How to achieve this, i have no clue about this.

<?php 
echo timespan(strtotime($row['abscondingsince']), 
               strtotime($row['dateofcontactviaphone']));
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
shank
  • 363
  • 1
  • 7
  • 23
  • `$diff = (new DateTime($row['abscondingsince']))->diff(new DateTime($row['dateofcontactviaphone']))->days;` – Mark Baker Aug 24 '16 at 12:31
  • timespan() function always displays the time difference in the format you are getting. You can try using this - – Vivek Srivastava Aug 24 '16 at 12:33
  • someone have already answered hope help you, check below url, you just need to modify according to your requirement. http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago – Ravi Shankar Aug 24 '16 at 12:34
  • awesome @ Mark Baker. Thank you. It worked without complicated code – shank Aug 24 '16 at 12:37

1 Answers1

0

I used below function in one of my codeignator function and it's working fine to me hope help you.

echo show_date_string('12-07-2016');
function show_date_string($datetime) {
    $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]);
        }
    }

    return $string ? implode(', ', $string) . ' ago' : 'just now';
}
Ravi Shankar
  • 1,559
  • 1
  • 11
  • 15
  • Don't post link only answers, quote the relevant part. And in this case it would be just a comment sharing the Stackoverflow question. – Olaf Dietsche Aug 24 '16 at 12:33