Skip to content

Instantly share code, notes, and snippets.

@kamel3d
Created June 7, 2020 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamel3d/d4cc857c2d53751e1a8987e61f8d40ba to your computer and use it in GitHub Desktop.
Save kamel3d/d4cc857c2d53751e1a8987e61f8d40ba to your computer and use it in GitHub Desktop.
The function relativedate() take date as argument in ISO 8601 format and returns an array with Arabic text that has the relative time since the time of the input and the other value in the array tells if the post is new or not, it is considered as new if the input is less than 4 hours old
<?php
/*
Copyright 2020 KAMEL LABIAD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
The function relativedate() take date as argument in ISO 8601 format and returns an array with Arabic text
that has the relative time since the time of the input and the other value in the array tells if the post
is new or not, it is considered as new if the input is less than 4 hours old
*/
function relativedate($postDate){
$today = date("Y-m-d H:i:s");
$timedif = strtotime($today)-strtotime($postDate);
$oneminute = 60; // 1 minute
$towminutes = 120; // 2 minutes
$threeminutes = 180; // 3 minutes
$elevenminutes = 660; // 11 minutes
$onehour = 3600; // 1 hour
$towhours = 7200; // 2 hours
$threehours = 10800; // 3 hours
$fourhours = 14400; // 4 hours
$elevenhours = 39600; // 11 hours
$Twentyfourhours = 86400; // 24 hours
$timeStamp['new'] = false;
// one minute
if ($timedif < $towminutes) {
$timeStamp['time'] = ' قبل دقيقة واحدة';
$timeStamp['new'] = true;
}
// tow minutes
if ($timedif >= $towminutes && $timedif < $threeminutes) {
$timeStamp['time'] = ' قبل دقيقتين';
$timeStamp['new'] = true;
}
// three minutes or more
if ($timedif >= $threeminutes && $timedif < $elevenminutes) {
$timeStamp['time'] = ' قبل '. floor($timedif/60) . ' دقائق ';
$timeStamp['new'] = true;
}
// eleven minutes and more
if ($timedif >= $elevenminutes && $timedif < $onehour) {
$timeStamp['time'] = ' قبل '. floor($timedif/60) . ' دقيقة ';
$timeStamp['new'] = true;
}
// one hour
if ($timedif >= $onehour && $timedif < $towhours) {
$timeStamp['time'] = ' قبل ساعة ';
$timeStamp['new'] = true;
}
// tow hours
if ($timedif >= $towhours && $timedif < $threehours) {
$timeStamp['time'] = ' قبل ساعتين ';
$timeStamp['new'] = true;
}
// three hours or more
if ($timedif >= $threehours && $timedif < $elevenhours) {
$timeStamp['time'] = ' قبل '. floor($timedif/3600) . ' ساعات ';
if ($timedif < $fourhours)
{
$timeStamp['new'] = true;
}
}
// eleven hours or more
if ($timedif >= $elevenhours && $timedif < $Twentyfourhours) {
$timeStamp['time'] = ' قبل '. floor($timedif/3600) . ' ساعة ';
}
// more than 24 hours start showing the post date in arabic
if ($timedif >= $Twentyfourhours ) {
$timeStamp['time'] = arabicDate($postDate);
}
return $timeStamp;
}
function arabicDate($rawDate){
// construct arabic dates with arabic months
$month = intval(date("m",strtotime($rawDate)));
$day = intval(date("d",strtotime($rawDate)));
$year = date("Y",strtotime($rawDate));
$monthsArabic = array(
1=>'يناير',
2=>'فبراير',
3=>'مارس',
4=>'أبريل',
5=>'مايو',
6=>'يونيو',
7=>'يوليو',
8=>'أغسطس',
9=>'سبتمبر',
10=>'أكتوبر',
11=>'نوفمبر',
12=>'ديسمبر'
);
$newDateMark =' '.$day.' '.$monthsArabic[$month].' '.$year;
return $newDateMark;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment