dimanche 9 août 2020

The if elseif else statements partially working with ternary operator

This is the part of my PHP code that is not running as I expected. The Returned text of the timestamp is always in the plural regardless of having '1' as the number of days, weeks or months. For instance, the website is displaying: Last physical activity perfomed 1 weeks ago. I would appreciate an advice.

/**

 * Creates the text of the timestamp.

 *

 * @param int|string Unix timestamp or string date format.

 *

 * @return string Returns the text of the time stamp.

 */

public static function textTimeStamp($mTime)

{

    if (is_string($mTime)) {

        // Converting the date string format into timeStamp

        $mTime = strtotime($mTime);

    }




    $iSeconds = time() - $mTime;




    $iMinutes = round($iSeconds / 60);

    $iHours = round($iSeconds / 3600);

    $iDays = round($iSeconds / 86400);

    $iWeeks = round($iSeconds / 604800);

    $iMonths = round($iSeconds / 2419200);

    $iYears = round($iSeconds / 29030400);




    if ($iSeconds === 0)

        $sTxt = t('%0% seconds ago.', 0.5);

    elseif ($iSeconds < 60)

        $sTxt = t('%0% seconds ago.', $iSeconds);

    elseif ($iMinutes < 60)

        $sTxt = $iMinutes === 1 ? t('one minute ago.') : t('%0% minutes ago.', $iMinutes);

    elseif ($iHours < 24)

        $sTxt = $iHours === 1 ? t('one hour ago.') : t('%0% hours ago.', $iHours);

    else

        if ($iDays < 7)

            $sTxt = $iDays === 1 ? t('one day ago.') : t('%0% days ago.', $iDays);

        elseif ($iWeeks < 4)

            $sTxt = $iWeeks === 1 ? t('one week ago.') : t('%0% weeks ago.', $iWeeks);

        elseif ($iMonths < 12)

            $sTxt = $iMonths === 1 ? t('one month ago.') : t('%0% months ago.', $iMonths);

        else

            $sTxt = $iYears === 1 ? t('one year ago.') : t('%0% years ago.', $iYears);




    return $sTxt;

}

Aucun commentaire:

Enregistrer un commentaire