Search code examples
phparabicdate-formatting

A Simple PHP function to format Arabic date to be user friendly and more human readable (Twitter Like)


A simple PHP function to format Arabic dates to be user friendly and more human readable (Twitter Like).


Solution

  • Here is the function..

    function arabic_date_format($timestamp)
    {
        $periods = array(
            "second"  => "ثانية", 
            "seconds" => "ثواني", 
            "minute"  => "دقيقة", 
            "minutes" => "دقائق", 
            "hour"    => "ساعة", 
            "hours"   => "ساعات", 
            "day"     => "يوم", 
            "days"    => "أيام", 
            "month"   => "شهر", 
            "months"  => "شهور", 
        );
    
        $difference = (int) abs(time() - $timestamp);
    
        $plural = array(3,4,5,6,7,8,9,10);
    
        $readable_date = "منذ ";
    
        if ($difference < 60) // less than a minute
        { 
            $readable_date .= $difference . " ";
            if (in_array($difference, $plural)) {
                $readable_date .= $periods["seconds"];
            } else {
                $readable_date .= $periods["second"];
            }
        } 
        elseif ($difference < (60*60)) // less than an hour
        { 
            $diff = (int) ($difference / 60);
            $readable_date .= $diff . " ";
            if (in_array($diff, $plural)) {
                $readable_date .= $periods["minutes"];
            } else {
                $readable_date .= $periods["minute"];
            }
        } 
        elseif ($difference < (24*60*60)) // less than a day
        { 
            $diff = (int) ($difference / (60*60));
            $readable_date .= $diff . " ";
            if (in_array($diff, $plural)) {
                $readable_date .= $periods["hours"];
            } else {
                $readable_date .= $periods["hour"];
            }
        } 
        elseif ($difference < (30*24*60*60)) // less than a month
        { 
            $diff = (int) ($difference / (24*60*60));
            $readable_date .= $diff . " ";
            if (in_array($diff, $plural)) {
                $readable_date .= $periods["days"];
            } else {
                $readable_date .= $periods["day"];
            }
        } 
        elseif ($difference < (365*24*60*60)) // less than a year
        { 
            $diff = (int) ($difference / (30*24*60*60));
            $readable_date .= $diff . " ";
    
            if (in_array($diff, $plural)) {
                $readable_date .= $periods["months"];
            } else {
                $readable_date .= $periods["month"];
            }
        } 
        else 
        {
            $readable_date = date("d-m-Y", $timestamp);
        }
    
        return $readable_date;
    }
    

    And here are some examples..

    echo arabic_date_format(strtotime("1 second ago")); // outputs "منذ 1 ثانية"
    echo arabic_date_format(strtotime("2 hours ago")); // outputs "منذ 2 ساعة"
    echo arabic_date_format(strtotime("7 hours ago")); // outputs "منذ 7 ساعات"