0

When using this answer from this thread. It fails for me, I checked the $now, $ago, $diff and $string and they all work or in proper condition.

public static function convertTime($datetime)
{
    $now = new DateTime;
    $now->setTimezone(new DateTimeZone('Asia/Manila'));
    $now->format('Y-m-d h:i:s');

    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = [
      '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]);
        }
    }
}

Why does it fail:

If I var_dump($diff->$k), it returns all the value to me is 0, making it null; the only problem is why?

3 Answers3

0

Perhaps you forget to return the value?

mokamoto12
  • 383
  • 1
  • 6
0

Your method needs to return a value, this could be the main reason why it currently returns NULL. However, you should as well create a whole new string for your output, as you currently seem to override $v every time you loop through the $string array. I've as well reformatted the output, as some whitespace was missing between the words.

By the way: you don't need to unset $string[$k] as you will loop only once through the array anyway. So that doesn't change anything.

This worked fine for me:

public static function convertTime($datetime)
{
    $now = new DateTime;
    $now->setTimezone(new DateTimeZone('Asia/Manila'));
    $now->format('Y-m-d h:i:s');

    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = [
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second'
    ];
    $result = '';
    foreach ($string as $k => $v) {
        if ($diff->$k) {
            $result .= $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's ' : ' ');
        }
    }

    return $result;
}
webFashion
  • 748
  • 9
  • 17
  • The problem now is I get the value but it doesn't show hour – knawledgelol Jul 23 '17 at 03:09
  • @knawledgelol It doesn't show the value if the value equals to zero. So in your case the hour value seems to be equal to zero. You can remove the `if($diff->$k)` clause (but not the content of it!) if you don't want or need that. – webFashion Jul 23 '17 at 03:15
0

look at the code:

function ego($date) { 
$delta = time() - strtotime($date);
if ( $delta < 60 && $delta >= 0)$data =  strval(round(($delta),0)). ' second ego';
elseif ($delta >= 60 && $delta < (60*60))$data = strval(round(($delta/60),0)). ' minute ego';
elseif ($delta >= (60*60) && $delta < (24*60*60))$data = strval(round(($delta/3600),0)). ' houre ego';
elseif ($delta >= (24*60*60) && $delta < (24*60*60*365))$data =  strval(round(($delta/86400),0)). ' day ego';
elseif ($delta >= (24*60*60*365)) $data = strval(round(($delta/31536000),0)). ' year ego';
return $data;
}
  • Please explain why your code should be 'looked at', try and show why your answer solves the original question. – Nigel Ren Jul 23 '17 at 06:31