Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2019
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.43 KB | None | 0 0
  1. <?php namespace System\Helpers;
  2.  
  3. use Carbon\Carbon;
  4. use DateTime as PhpDateTime;
  5. use InvalidArgumentException;
  6. use Exception;
  7.  
  8. class DateTime
  9. {
  10.     /**
  11.      * Returns a human readable time difference from the value to the
  12.      * current time. Eg: **10 minutes ago**
  13.      *
  14.      * @return string
  15.      */
  16.     public static function timeSince($datetime)
  17.     {
  18.         return trans(self::makeCarbon($datetime)->diffForHumans());
  19.     }
  20.  
  21.     /**
  22.      * Returns 24-hour time and the day using the grammatical tense
  23.      * of the current time. Eg: Today at 12:49, Yesterday at 4:00
  24.      * or 18 Sep 2015 at 14:33.
  25.      *
  26.      * @return string
  27.      */
  28.     public static function timeTense($datetime)
  29.     {
  30.         $datetime = self::makeCarbon($datetime);
  31.         $yesterday = $datetime->subDays(1);
  32.         $tomorrow = $datetime->addDays(1);
  33.         $time = $datetime->format('H:i');
  34.         $date = $datetime->format('j M Y');
  35.  
  36.         if ($datetime->isToday()) {
  37.             $date = 'Today';
  38.         }
  39.         elseif ($datetime->isYesterday()) {
  40.             $date = 'Yesterday';
  41.         }
  42.         elseif ($datetime->isTomorrow()) {
  43.             $date = 'Tomorrow';
  44.         }
  45.  
  46.         return $date.' at '.$time;
  47.     }
  48.  
  49.     /**
  50.      * Converts mixed inputs to a Carbon object.
  51.      *
  52.      * @return Carbon\Carbon
  53.      */
  54.     public static function makeCarbon($value, $throwException = true)
  55.     {
  56.         if ($value instanceof Carbon) {
  57.             // Do nothing
  58.         }
  59.         elseif ($value instanceof PhpDateTime) {
  60.             $value = Carbon::instance($value);
  61.         }
  62.         elseif (is_numeric($value)) {
  63.             $value = Carbon::createFromTimestamp($value);
  64.         }
  65.         elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) {
  66.             $value = Carbon::createFromFormat('Y-m-d', $value)->startOfDay();
  67.         }
  68.         else {
  69.             try {
  70.                 $value = Carbon::parse($value);
  71.             } catch (Exception $ex) {
  72.             }
  73.         }
  74.  
  75.         if (!$value instanceof Carbon && $throwException) {
  76.             throw new InvalidArgumentException('Invalid date value supplied to DateTime helper.');
  77.         }
  78.  
  79.         return $value;
  80.     }
  81.  
  82.     /**
  83.      * Converts a PHP date format to "Moment.js" format.
  84.      * @param string $format
  85.      * @return string
  86.      */
  87.     public static function momentFormat($format)
  88.     {
  89.         $replacements = [
  90.             'd' => 'DD',
  91.             'D' => 'ddd',
  92.             'j' => 'D',
  93.             'l' => 'dddd',
  94.             'N' => 'E',
  95.             'S' => 'o',
  96.             'w' => 'e',
  97.             'z' => 'DDD',
  98.             'W' => 'W',
  99.             'F' => 'MMMM',
  100.             'm' => 'MM',
  101.             'M' => 'MMM',
  102.             'n' => 'M',
  103.             't' => '', // no equivalent
  104.             'L' => '', // no equivalent
  105.             'o' => 'YYYY',
  106.             'Y' => 'YYYY',
  107.             'y' => 'YY',
  108.             'a' => 'a',
  109.             'A' => 'A',
  110.             'B' => '', // no equivalent
  111.             'g' => 'h',
  112.             'G' => 'H',
  113.             'h' => 'hh',
  114.             'H' => 'HH',
  115.             'i' => 'mm',
  116.             's' => 'ss',
  117.             'u' => 'SSS',
  118.             'e' => 'zz', // deprecated since version 1.6.0 of moment.js
  119.             'I' => '', // no equivalent
  120.             'O' => '', // no equivalent
  121.             'P' => '', // no equivalent
  122.             'T' => '', // no equivalent
  123.             'Z' => '', // no equivalent
  124.             'c' => '', // no equivalent
  125.             'r' => '', // no equivalent
  126.             'U' => 'X',
  127.         ];
  128.  
  129.         foreach ($replacements as $from => $to) {
  130.             $replacements['\\'.$from] = '['.$from.']';
  131.         }
  132.  
  133.         return strtr($format, $replacements);
  134.     }
  135.  
  136.     public static function trans($date = null){
  137.  
  138.         $months = array(
  139.             "Jan" => "يناير",
  140.             "Feb" => "فبراير",
  141.             "Mar" => "مارس",
  142.             "Apr" => "أبريل",
  143.             "May" => "مايو",
  144.             "Jun" => "يونيو",
  145.             "Jul" => "يوليو",
  146.             "Aug" => "أغسطس",
  147.             "Sep" => "سبتمبر",
  148.             "Oct" => "أكتوبر",
  149.             "Nov" => "نوفمبر",
  150.             "Dec" => "ديسمبر"
  151.         );
  152.  
  153.         $month = date("M", strtotime($date));
  154.         $day = date("d", strtotime($date));
  155.         $year = date("Y", strtotime($date));
  156.         $ar_month = $months[$month];
  157.         return  "<p dir='rtl'>".$day." ".$ar_month." ".$year."</p>";
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement