Last active
August 29, 2016 01:02
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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