0

hi i want to exclude sat and sun from this function.currently this is calculating all days

function human_timing_mins_hrs_days_only($time)
{
    $time = time() - $time; // to get the time since that moment
    $tokens = array (
        86400 => 'day',
        3600 => 'hour',
        60 => 'min',
        0 => 'min'
    );
    foreach ($tokens as $unit => $text) {
        if ($time < $unit) continue;
        $numberOfUnits = floor($time / $unit);
        return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
    }
}
Strawberry
  • 33,750
  • 13
  • 40
  • 57
m.usman
  • 43
  • 7

1 Answers1

0

You can try it using the datetime object:

function human_timing_mins_hrs_days_only($time)
{
    $now = new DateTime();
    $dateInThePast = (new DateTime)->setTimestamp($time);
    $interval = $now->diff($dateInThePast, true);

    $sundays = intval($interval->days / 7) + ($now->format('N') + $interval->days % 7 >= 7);
    $saturdays = intval($interval->days / 7) + ($now->format('N') + $interval->days % 6 >= 6);

    $tokens = array (
        'days' => 'day',
        'h'    => 'hour',
        'i'    => 'min',
    );

    $units = 0;
    $text = 'mins';

    foreach ($tokens as $property => $name) {
        if ($interval->$property === 0) {
            continue;
        }

        $units = $property === 'days' ? $interval->$property - ($sundays + $saturdays) : $interval->$property;
        $text = $name . ($units > 1 ? 's' : '');
    }

    return $units . ' ' . $text;
}

Please note that this code is not tested and may be required some updates in order to make it work properly.

Mihai Matei
  • 24,166
  • 5
  • 32
  • 50