Requires PHP >= 5.2 PHP's default 'date' function does not support years higher than 2038. Intorduced in PHP5, DateTime class supports higher years and also invalid date entries. Also, Persian users are using classic 'jdate' function for years now and beside the fact that it's amazing and also helped me to write this one, it's really out of date, and can't be used in modern real world web applications as it is completely written in functions. Copyright (C) 2012 Sallar Kaboli (http://sallar.me) Part of Phoenix Framework (p5x.org) by Phoenix Alternatvie Original Jalali to Gregorian (and vice versa) convertor: Copyright (C) 2000 Roozbeh Pournader and Mohammad Toossi List of supported timezones can be found here: http://www.php.net/manual/en/timezones.php PHP version 5 LICENSE: This source file is subject to version 3.01 of the PHP license that is available through the world-wide-web at the following URI: http://www.php.net/license/3_01.txt. If you did not receive a copy of the PHP License and are unable to obtain it through the web, please send a note to license@php.net so we can mail you a copy immediately.
See also: DateTime
Author: Sallar Kaboli (sallar.kaboli@gmail.com)
Author: Omid Pilevar (omid.pixel@gmail.com)
Ejemplo n.º 1
1
 public function test_parseFromPersian()
 {
     $jalaliDate = '1393/03/27';
     $date = jDateTime::parseFromFormat('Y/m/d', $jalaliDate);
     $this->assertEquals(1393, $date['year']);
     $this->assertEquals(03, $date['month']);
     $this->assertEquals(27, $date['day']);
     $date = jDateTime::parseFromFormat('Y-m-d H:i:s', '1395-03-15 21:00:00');
     $this->assertEquals(21, $date['hour']);
     $this->assertEquals(0, $date['minute']);
     $this->assertEquals(0, $date['second']);
 }
Ejemplo n.º 2
0
 /**
  * @param $format
  * @return bool|string
  */
 public function format($format)
 {
     // convert alias string
     if (in_array($format, array_keys($this->formats))) {
         $format = $this->formats[$format];
     }
     // if valid unix timestamp...
     if ($this->dateTime !== false) {
         return jDateTime::strftime($format, $this->dateTime->getTimestamp(), $this->dateTime->getTimezone());
     } else {
         return false;
     }
 }
Ejemplo n.º 3
0
 public function test_format_with_convert_to_persian()
 {
     $object = new jDate();
     $jDate = $object->forge('2015-06-13')->format('Y-m-d');
     $this->assertTrue('۱۳۹۴-۰۳-۲۳' === \Morilog\Jalali\jDateTime::convertNumbers($jDate));
 }
Ejemplo n.º 4
0
 public function setStartDateAttribute($start_date)
 {
     $this->attributes['start_date'] = jDateTime::jtosql($start_date);
 }
Ejemplo n.º 5
0
 public function setExpiredAtAttribute($date)
 {
     $jalali = explode('/', $date);
     $this->attributes['expired_at'] = implode('-', jDateTime::toGregorian($jalali[0], $jalali[1], $jalali[2]));
 }
Ejemplo n.º 6
0
 public function setEstimateDateAttribute($estimate_date)
 {
     $this->attributes['estimate_date'] = jDateTime::jtosql($estimate_date);
 }
Ejemplo n.º 7
0
 /**
  * @param $format
  * @param $date
  * @return array
  */
 public static function parseFromFormat($format, $date)
 {
     // reverse engineer date formats
     $keys = array('Y' => array('year', '\\d{4}'), 'y' => array('year', '\\d{2}'), 'm' => array('month', '\\d{2}'), 'n' => array('month', '\\d{1,2}'), 'M' => array('month', '[A-Z][a-z]{3}'), 'F' => array('month', '[A-Z][a-z]{2,8}'), 'd' => array('day', '\\d{2}'), 'j' => array('day', '\\d{1,2}'), 'D' => array('day', '[A-Z][a-z]{2}'), 'l' => array('day', '[A-Z][a-z]{6,9}'), 'u' => array('hour', '\\d{1,6}'), 'h' => array('hour', '\\d{2}'), 'H' => array('hour', '\\d{2}'), 'g' => array('hour', '\\d{1,2}'), 'G' => array('hour', '\\d{1,2}'), 'i' => array('minute', '\\d{2}'), 's' => array('second', '\\d{2}'));
     // convert format string to regex
     $regex = '';
     $chars = str_split($format);
     foreach ($chars as $n => $char) {
         $lastChar = isset($chars[$n - 1]) ? $chars[$n - 1] : '';
         $skipCurrent = '\\' == $lastChar;
         if (!$skipCurrent && isset($keys[$char])) {
             $regex .= '(?P<' . $keys[$char][0] . '>' . $keys[$char][1] . ')';
         } else {
             if ('\\' == $char) {
                 $regex .= $char;
             } else {
                 $regex .= preg_quote($char);
             }
         }
     }
     $dt = array();
     $dt['error_count'] = 0;
     // now try to match it
     if (preg_match('#^' . $regex . '$#', $date, $dt)) {
         foreach ($dt as $k => $v) {
             if (is_int($k)) {
                 unset($dt[$k]);
             }
         }
         if (!jDateTime::checkdate($dt['month'], $dt['day'], $dt['year'], false)) {
             $dt['error_count'] = 1;
         }
     } else {
         $dt['error_count'] = 1;
     }
     $dt['errors'] = array();
     $dt['fraction'] = '';
     $dt['warning_count'] = 0;
     $dt['warnings'] = array();
     $dt['is_localtime'] = 0;
     $dt['zone_type'] = 0;
     $dt['zone'] = 0;
     $dt['is_dst'] = '';
     if (strlen($dt['year']) == 2) {
         $now = self::forge('now');
         $x = $now->format('Y') - $now->format('y');
         $dt['year'] += $x;
     }
     $dt['year'] = isset($dt['year']) ? (int) $dt['year'] : 0;
     $dt['month'] = isset($dt['month']) ? (int) $dt['month'] : 0;
     $dt['day'] = isset($dt['day']) ? (int) $dt['day'] : 0;
     $dt['hour'] = isset($dt['hour']) ? (int) $dt['hour'] : 0;
     $dt['minute'] = isset($dt['minute']) ? (int) $dt['minute'] : 0;
     $dt['second'] = isset($dt['second']) ? (int) $dt['second'] : 0;
     return $dt;
 }