public static date ( $format, $stamp = false, $convert = null, $jalali = null, $timezone = null ) : string | ||
$format | string Acceps format string based on: php.net/date | |
$stamp | int Unix Timestamp (Epoch Time) | |
$convert | bool (Optional) forces convert action. pass null to use system default | |
$jalali | bool (Optional) forces jalali conversion. pass null to use system default | |
$timezone | string (Optional) forces a different timezone. pass null to use system default | |
return | string | Formatted input |
public static function date($format, $stamp = false, $convert = null, $jalali = null, $timezone = null)
{
//Timestamp + Timezone
$stamp = $stamp !== false ? $stamp : time();
$timezone = $timezone != null ? $timezone : (self::$timezone != null ? self::$timezone : date_default_timezone_get());
$obj = new DateTime('@' . $stamp, new DateTimeZone($timezone));
$obj->setTimezone(new DateTimeZone($timezone));
if (self::$jalali === false && $jalali === null || $jalali === false) {
return $obj->format($format);
} else {
//Find what to replace
$chars = preg_match_all('/([a-zA-Z]{1})/', $format, $chars) ? $chars[0] : array();
//Intact Keys
$intact = array('B', 'h', 'H', 'g', 'G', 'i', 's', 'I', 'U', 'u', 'Z', 'O', 'P');
$intact = self::filterArray($chars, $intact);
$intactValues = array();
foreach ($intact as $k => $v) {
$intactValues[$k] = $obj->format($v);
}
//End Intact Keys
//Changed Keys
list($year, $month, $day) = array($obj->format('Y'), $obj->format('n'), $obj->format('j'));
list($jyear, $jmonth, $jday) = self::toJalali($year, $month, $day);
$keys = array('d', 'D', 'j', 'l', 'N', 'S', 'w', 'z', 'W', 'F', 'm', 'M', 'n', 't', 'L', 'o', 'Y', 'y', 'a', 'A', 'c', 'r', 'e', 'T');
$keys = self::filterArray($chars, $keys, array('z'));
$values = array();
foreach ($keys as $k => $key) {
$v = '';
switch ($key) {
//Day
case 'd':
$v = sprintf('%02d', $jday);
break;
case 'D':
$v = self::getDayNames($obj->format('D'), true);
break;
case 'j':
$v = $jday;
break;
case 'l':
$v = self::getDayNames($obj->format('l'));
break;
case 'N':
$v = self::getDayNames($obj->format('l'), false, 1, true);
break;
case 'S':
$v = 'ام';
break;
case 'w':
$v = self::getDayNames($obj->format('l'), false, 1, true) - 1;
break;
case 'z':
if ($jmonth > 6) {
$v = 186 + ($jmonth - 6 - 1) * 30 + $jday;
} else {
$v = ($jmonth - 1) * 31 + $jday;
}
self::$temp['z'] = $v;
break;
//Week
//Week
case 'W':
$v = is_int(self::$temp['z'] / 7) ? self::$temp['z'] / 7 : intval(self::$temp['z'] / 7 + 1);
break;
//Month
//Month
case 'F':
$v = self::getMonthNames($jmonth);
break;
case 'm':
$v = sprintf('%02d', $jmonth);
break;
case 'M':
$v = self::getMonthNames($jmonth, true);
break;
case 'n':
$v = $jmonth;
break;
case 't':
if ($jmonth >= 1 && $jmonth <= 6) {
$v = 31;
} else {
if ($jmonth >= 7 && $jmonth <= 11) {
$v = 30;
} else {
if ($jmonth == 12 && $jyear % 4 == 3) {
$v = 30;
} else {
if ($jmonth == 12 && $jyear % 4 != 3) {
$v = 29;
}
}
}
}
break;
//Year
//Year
case 'L':
$tmpObj = new DateTime('@' . (time() - 31536000));
$v = $tmpObj->format('L');
break;
case 'o':
case 'Y':
$v = $jyear;
break;
case 'y':
$v = $jyear % 100;
break;
//Time
//Time
case 'a':
$v = $obj->format('a') == 'am' ? 'ق.ظ' : 'ب.ظ';
break;
case 'A':
$v = $obj->format('A') == 'AM' ? 'قبل از ظهر' : 'بعد از ظهر';
break;
//Full Dates
//Full Dates
case 'c':
$v = $jyear . '-' . sprintf('%02d', $jmonth) . '-' . sprintf('%02d', $jday) . 'T';
$v .= $obj->format('H') . ':' . $obj->format('i') . ':' . $obj->format('s') . $obj->format('P');
break;
case 'r':
$v = self::getDayNames($obj->format('D'), true) . ', ' . sprintf('%02d', $jday) . ' ' . self::getMonthNames($jmonth, true);
$v .= ' ' . $jyear . ' ' . $obj->format('H') . ':' . $obj->format('i') . ':' . $obj->format('s') . ' ' . $obj->format('P');
break;
//Timezone
//Timezone
case 'e':
$v = $obj->format('e');
break;
case 'T':
$v = $obj->format('T');
break;
}
$values[$k] = $v;
}
//End Changed Keys
//Merge
$keys = array_merge($intact, $keys);
$values = array_merge($intactValues, $values);
//Return
$ret = strtr($format, array_combine($keys, $values));
return $convert === false || $convert === null && self::$convert === false || ($jalali === false || $jalali === null && self::$jalali === false) ? $ret : self::convertNumbers($ret);
}
}
public function test_create() { // $date = new Date(); // echo $date->date("W"); // $date = new jDateTime(false, true, 'Asia/Tehran'); // echo $date->date("z W",time()); // echo $date->date("Y m d",strtotime("next week"))."<br>"; // echo jdate("Y m d",strtotime("next week"))."<br>"; // echo $date->strftime("y m d","next week"); // echo $date->strftime("next week", time(),true, true, 'America/New_York'); $this->load->library("date"); $date1 = new jDateTime(false, true, 'Asia/Tehran'); $date2 = new jDateTime(false); $date1->strftime("now"); $date2->strftime("tomorrow"); echo $date1->strftime("%s"); var_dump($date1->date() == $date2->date()); var_dump($date1 < $date2); var_dump($date1 > $date2); }