一个简单的 PHP 函数,用于格式化阿拉伯日期以方便用户使用和更易于阅读(喜欢 Twitter)

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

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

试试 intldateformatter(阅读此处 http://uk.php.net/manual/en/class.intldateformatter.php)。

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

这是函数..

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