Пример #1
0
 /**
  * Get the difference in a human readable format.
  *
  * @param  Date    $since
  * @param  bool    $absolute removes time difference modifiers ago, after, etc
  * @return string
  */
 public function diffForHumans(Carbon $since = null, $absolute = false)
 {
     // Get translator
     $lang = $this->getTranslator();
     // Are we comparing against another date?
     $relative = !is_null($since);
     if (is_null($since)) {
         $since = new static('now', $this->getTimezone());
     }
     // Are we comparing to a date in the future?
     $future = $since->getTimestamp() < $this->getTimestamp();
     $units = array('second' => 60, 'minute' => 60, 'hour' => 24, 'day' => 7, 'week' => 30 / 7, 'month' => 12);
     // Date difference
     $difference = abs($since->getTimestamp() - $this->getTimestamp());
     // Default unit
     $unit = 'year';
     // Select the best unit.
     foreach ($units as $key => $value) {
         if ($difference < $value) {
             $unit = $key;
             break;
         }
         $difference = $difference / $value;
     }
     $difference = floor($difference);
     // Select the suffix.
     if ($relative) {
         $suffix = $future ? 'after' : 'before';
     } else {
         $suffix = $future ? 'from_now' : 'ago';
     }
     // Some languages have different unit translations when used in combination
     // with a specific suffix. Here we will check if there is an optional
     // translation for that specific suffix and use it if it exists.
     if ($lang->get("date::date.{$unit}_diff") != "date::date.{$unit}_diff") {
         $ago = $lang->choice("date::date.{$unit}_diff", $difference);
     } else {
         if ($lang->get("date::date.{$unit}_{$suffix}") != "date::date.{$unit}_{$suffix}") {
             $ago = $lang->choice("date::date.{$unit}_{$suffix}", $difference);
         } else {
             $ago = $lang->choice("date::date.{$unit}", $difference);
         }
     }
     if ($absolute) {
         return $ago;
     }
     return $lang->choice("date::date.{$suffix}", $difference, array('time' => $ago));
 }
Пример #2
0
 /**
  * Get the difference in a human readable format.
  *
  * @param  Date   $since
  * @param  bool   $absolute Removes time difference modifiers ago, after, etc
  * @return string
  */
 public function diffForTheiums(Carbon $since = null, $absolute = false)
 {
     // Get translator
     $lang = $this->getTranslator();
     if (is_null($since)) {
         $since = new static('now', $this->getTimezone());
     }
     // Date difference
     $difference = abs($since->getTimestamp() - $this->getTimestamp());
     $suffix = "before";
     // Default unit
     $unit = 'year';
     if ($difference <= 10) {
         return "Birkaç saniye önce";
     } else {
         if ($difference < 60) {
             $ago = $lang->transChoice("second", $difference, array(':count' => $difference));
         } else {
             if ($difference < 3600) {
                 $difference = floor($difference / 60);
                 $ago = $lang->transChoice("minute", $difference, array(':count' => $difference));
             } else {
                 if ($difference < 86400) {
                     $difference = floor($difference / 60 / 60);
                     $ago = $lang->transChoice("hour", $difference, array(':count' => $difference));
                 } else {
                     if ($difference < 172800) {
                         return $lang->transChoice("yesterday", $difference, array(':count' => $difference)) . ", " . $this->format('H:i');
                     } else {
                         if ($difference < 345600) {
                             return $lang->transChoice("yesterday", $difference, array(':count' => $difference)) . ", " . $this->format('H:i');
                         } else {
                             if ($difference < 28512000) {
                                 return $this->format('j F, H:i');
                             } else {
                                 return $this->format('j F Y, H:i');
                             }
                         }
                     }
                 }
             }
         }
     }
     return $lang->transChoice($suffix, $difference, array(':time' => $ago));
     //Dil seçeneği için uğraşmaya devam etmelisin. Buradaki sistemi anlaman lazım.
     /*
     $result = "";
     if ($difference <= 10){
         $result = "Birkaç ".Lang::get("tags.second")." ".Lang::get("tags.ago");
     }else if($difference < 60){
         $result = $difference." ".Lang::get("tags.second")." ".Lang::get("tags.ago");
     }else if($difference < 3600){
         $minute = floor($difference / 60);
         $result = $minute." ".Lang::get("tags.minute")." ".Lang::get("tags.ago");
     }else if($difference < 86400){
         $hour = floor($difference / 60 / 60);
         $result = $hour." ".Lang::get("tags.hour")." ".Lang::get("tags.ago");
     }else if($difference < 172800){
         $result = Lang::get("tags.yesterday").", ".$this->format('H:i');
     }else if($difference < 345600){
         $result = Lang::get("tags.day_before_yesterday").", ".$this->format('H:i');
     }else if($difference < 28512000){
         $result = $this->format('j F, H:i');
     }else{
         $result = $this->format('j F Y, H:i');
     }
     return $result;
     */
 }
Пример #3
0
 /**
  * Convert a timestamp string to a given format.
  *
  * @param int $style Constant Output format for timestamp
  * @param string $ts Timestamp
  * @return string|bool Formatted timestamp or false on failure
  */
 public static function convert($style = TS_UNIX, $ts)
 {
     try {
         $ct = new static($ts);
         return $ct->getTimestamp($style);
     } catch (TimestampException $e) {
         return false;
     }
 }
Пример #4
0
 /**
  * Returns new Date object formatted according to the specified format. The
  * method supports both strptime() formats and DateTime formats
  *
  * @param  string               $format  any format supported by DateTime, or a format name configured
  * @param  string               $time    string representing the time.
  * @param  string|DateTimeZone  $time    timezone, if null the default timezone will be used
  *
  * @throws  Exception             if the timezone passed is not valid
  * @throws  OutOfBoundsException  if the input time passed is not valid
  *
  * @return  bool|Date  new Date instance, or false on failure
  */
 public function createFromFormat($format = 'local', $time, $timezone = null)
 {
     // deal with the timezone passed
     if ($timezone !== null) {
         if (!$timezone instanceof DateTimeZone) {
             $timezone = new DateTimeZone($timezone);
         }
     } else {
         $timezone = static::defaultTimezone();
     }
     // custom format pattern lookup
     if (array_key_exists($format, static::$patterns)) {
         $format = static::$patterns[$format];
     }
     // do we have a possible strptime() format to work with?
     if (strpos($format, '%') !== false and $timestamp = strptime($time, $format)) {
         // convert it into a timestamp
         $timestamp = mktime($timestamp['tm_hour'], $timestamp['tm_min'], $timestamp['tm_sec'], $timestamp['tm_mon'] + 1, $timestamp['tm_mday'], $timestamp['tm_year'] + 1900);
         if ($timestamp === false or $timestamp['unparsed']) {
             throw new \OutOfBoundsException('Input was invalid.' . (PHP_INT_SIZE == 4 ? ' A 32-bit system only supports dates between 1901 and 2038.' : ''));
         }
         // coversion was made in UTC, and strptime() does timezones but no daylight savings, so we might need a correction here
         if (($delta = static::defaultTimezone()->getOffset(new DateTime("2013-01-01 12:00:00", $timezone))) !== 0) {
             $time += $delta;
         }
         $format = 'U';
     }
     if ($datetime = DateTime::createFromFormat($format, $time, $timezone)) {
         // if we had a delta, we're working with UTC, so we have to do some adjusting again
         if (isset($delta)) {
             $delta = $timezone->getOffset($datetime);
             if ($delta < 0) {
                 $delta = new DateInterval('PT' . abs($delta) . 'S');
                 $delta->invert = 1;
             } else {
                 $delta = new DateInterval('PT' . $delta . 'S');
             }
             $datetime->sub($delta);
         }
         $datetime = new static('@' . $datetime->getTimestamp(), $timezone);
     }
     // update any errors found in the last DateTime static call
     static::$lastErrors = DateTime::getLastErrors();
     if (!$datetime) {
         return false;
     }
     return $datetime;
 }