1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

1 minute instead of 1 minutes, 1 hour instead of 1 hours, etc.

Discussion in 'PHP' started by qwikad.com, Oct 18, 2016.

  1. #1
    The script works fine, but I need to modify it to show 1 minute ago instead of 1 minutes ago, 1 hour ago instead of 1 hours ago and 1 day ago instead of 1 days ago. I tried this and that, but couldn't get it working right.

    
    $days = floor($diff/86400);
    $hours = floor(($diff-$days*86400)/(60 * 60));
    $min = floor(($diff-($days*86400+$hours*3600))/60);
    $second = $diff - ($days*86400+$hours*3600+$min*60);
    
    if($days > 0) echo $days." days ago";
    elseif($hours > 0) echo $hours." hours ago";
    elseif($min > 0) echo $min." minutes ago";
    else echo "Just now";
    
    Code (markup):

     
    Solved! View solution.
    qwikad.com, Oct 18, 2016 IP
  2. #2
    You could just put the plural into a greater than one condition.
    
    <?php
    
        $days = floor( $diff / 86400 );
        $hours = floor( ( $diff - $days * 86400 ) / 3600 );
        $min = floor( ( $diff - ( $days * 86400 + $hours * 3600 ) ) / 60 );
        $second = $diff - ( $days * 86400 + $hours * 3600 + $min * 60 );
    
        if ( $days )
            echo "$days day". ( $days > 1 ? 's' : '' ) ." ago";
        elseif ( $hours )
            echo "$hours hour". ( $hours > 1 ? 's' : '' ) ." ago";
        elseif ( $min )
            echo "$min minute". ( $min > 1 ? 's' : '' ) ." ago";
        else
            echo 'Just now';
    
    ?>
    
    Code (PHP):
     
    edduvs, Oct 18, 2016 IP
    qwikad.com likes this.