Skip to content

Instantly share code, notes, and snippets.

@AmrMekkawy
Last active August 29, 2016 01:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AmrMekkawy/065ee2f35cc0d7a23a20 to your computer and use it in GitHub Desktop.
Save AmrMekkawy/065ee2f35cc0d7a23a20 to your computer and use it in GitHub Desktop.
A PHP function to format Arabic date to be user friendly and more readable (Twitter Like)
<?php
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment