一个简单的PHP函数,将阿拉伯日期格式化为用户友好且更易于阅读(Twitter Like)


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

一个简单的PHP函数,将阿拉伯日期格式化为用户友好且更易于阅读(Twitter Like)。

函数是…

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;
}

下面是一些例子。

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 ساعات"

try intldateformatter(点击此处阅读http://uk.php.net/manual/en/class.intldateformatter.php) .

$formatter = new  IntlDateFormatter('ar_SA',IntlDateFormatter::FULL,IntlDateFormatter::FULL);
$date = new DateTime();
echo $date;