admin
10 years ago
plugins
11 years ago
widget
10 years ago
fixes-archive.php
11 years ago
fixes-dates.php
11 years ago
fixes-get_archives.php
11 years ago
fixes-get_calendar.php
11 years ago
fixes-misc.php
11 years ago
fixes-permalinks.php
11 years ago
general.php
10 years ago
install.php
11 years ago
parsidate.php
11 years ago
settings.php
10 years ago
parsidate.php
361 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Parsi date main conversation class |
| 4 | * |
| 5 | * @author Mobin Ghasempoor |
| 6 | * @package WP-Parsidate |
| 7 | * @subpackage DateConversation |
| 8 | */ |
| 9 | /*Special thanks to : |
| 10 | Reza Gholampanahi for convert function*/ |
| 11 | |
| 12 | class bn_parsidate |
| 13 | { |
| 14 | protected static $instance; |
| 15 | public $persian_month_names = array('','فروردین','اردیبهشت','خرداد','تیر','� |
| 16 | رداد','شهریور','� |
| 17 | هر','آبان','آذر','دی','به� |
| 18 | ن','اسفند'); |
| 19 | public $persian_short_month_names = array('','فرو','ارد','خرد','تیر','� |
| 20 | رد','شهر','� |
| 21 | هر','آبا','آذر','دی','به� |
| 22 | ','اسف'); |
| 23 | public $sesson = array('بهار','تابستان','پاییز','ز� |
| 24 | ستان'); |
| 25 | |
| 26 | public $persian_day_names = array('یکشنبه','دوشنبه','سه شنبه','چهارشنبه','پنجشنبه','ج� |
| 27 | عه','شنبه'); |
| 28 | public $persian_day_small = array('ی','د','س','چ','پ','ج','ش'); |
| 29 | |
| 30 | public $j_days_in_month = array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29); |
| 31 | private $j_days_sum_month = array(0,0,31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336); |
| 32 | |
| 33 | private $g_days_sum_month = array(0,0,31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334); |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Constructor |
| 38 | */ |
| 39 | function __construct(){ |
| 40 | if($this->IsLeapYear($this->persian_date("Y",time(),"eng"))){ |
| 41 | $this->j_days_in_month[11] = 30; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * bn_parsidate::getInstance() |
| 47 | * create instance of bn_parsidate class |
| 48 | * |
| 49 | * @return instance |
| 50 | */ |
| 51 | public static function getInstance() |
| 52 | { |
| 53 | if (!isset(self::$instance)) |
| 54 | self::$instance = new self(); |
| 55 | return self::$instance; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * bn_parsidate::IsLeapYear() |
| 60 | * check year is leap |
| 61 | * |
| 62 | * @param mixed $year |
| 63 | * @return boolean |
| 64 | */ |
| 65 | private function IsLeapYear($year) |
| 66 | { |
| 67 | if((($year%4)==0 && ($year%100)!=0)||(($year%400)==0)&&($year%100)==0) |
| 68 | return true; |
| 69 | else |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * bn_parsidate::persian_to_gregorian() |
| 75 | * convert persian date to gregorian date |
| 76 | * |
| 77 | * @param mixed $jy |
| 78 | * @param mixed $jm |
| 79 | * @param mixed $jd |
| 80 | * @return array |
| 81 | */ |
| 82 | public function persian_to_gregorian($jy,$jm,$jd) |
| 83 | { |
| 84 | $doyj = ($jm-2>-1?$this->j_days_sum_month[(int)$jm]+$jd:$jd); |
| 85 | $d4 = ($jy+1)%4; |
| 86 | $d33 = (int)((($jy-55)%132)*.0305); |
| 87 | $a = ($d33!=3 and $d4<=$d33)?287:286; |
| 88 | $b = (($d33==1 or $d33==2) and ($d33==$d4 or $d4==1))?78:(($d33==3 and $d4==0)?80:79); |
| 89 | if((int)(($jy-19)/63)==20) |
| 90 | {$a--;$b++;} |
| 91 | if($doyj<=$a){ |
| 92 | $gy = $jy+621; $gd=$doyj+$b; |
| 93 | }else{ |
| 94 | $gy = $jy+622; $gd=$doyj-$a; |
| 95 | } |
| 96 | foreach(array(0,31,($gy%4==0)?29:28,31,30,31,30,31,31,30,31,30,31) as $gm=>$days){ |
| 97 | if($gd<=$days)break; |
| 98 | $gd-=$days; |
| 99 | } |
| 100 | return array($gy,$gm,$gd); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * bn_parsidate::gregorian_to_persian() |
| 105 | * convert gregorian date to persian date |
| 106 | * |
| 107 | * @param mixed $gy |
| 108 | * @param mixed $gm |
| 109 | * @param mixed $gd |
| 110 | * @return array |
| 111 | */ |
| 112 | function gregorian_to_persian($gy,$gm,$gd) |
| 113 | { |
| 114 | $dayofyear = $this->g_days_sum_month[(int)$gm]+$gd; |
| 115 | if(self::IsLeapYear($gy) and $gm>2) |
| 116 | $dayofyear++; |
| 117 | $d_33 = (int)((($gy-16)%132)*0.0305); |
| 118 | $leap = $gy%4; |
| 119 | $a = (($d_33==1 or $d_33==2) and ($d_33==$leap or $leap==1))?78:(($d_33==3 and $leap==0)?80:79); |
| 120 | $b = ($d_33==3 or $d_33<($leap-1) or $leap==0)?286:287; |
| 121 | if((int)(($gy-10)/63)==30) |
| 122 | {$b--;$a++;} |
| 123 | if($dayofyear>$a){ |
| 124 | $jy = $gy-621; |
| 125 | $jd = $dayofyear-$a; |
| 126 | }else{ |
| 127 | $jy = $gy-622; |
| 128 | $jd = $dayofyear+$b; |
| 129 | } |
| 130 | for($i=0;$i<11 and $jd>$this->j_days_in_month[$i];$i++) |
| 131 | $jd-= $this->j_days_in_month[$i]; |
| 132 | $jm = ++$i; |
| 133 | return array($jy,strlen($jm)==1?'0'.$jm:$jm,strlen($jd)==1?'0'.$jd:$jd); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * bn_parsidate::trim_number() |
| 138 | * convert english number to persian number |
| 139 | * @param mixed $num |
| 140 | * @param string $sp |
| 141 | * @return string |
| 142 | */ |
| 143 | public function trim_number($num,$sp='٫') |
| 144 | { |
| 145 | $eng = array('0','1','2','3','4','5','6','7','8','9','.'); |
| 146 | $per = array('۰','۱','۲','۳','۴','۵','۶','۷','۸','۹',$sp); |
| 147 | $number = filter_var($num, FILTER_SANITIZE_NUMBER_INT); |
| 148 | return empty($number)?str_replace($per,$eng,$num):str_replace($eng,$per,$num); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * Get day of the week shamsi/jalali |
| 154 | * @author Parsa Kafi |
| 155 | * @param int $wday |
| 156 | * @return int |
| 157 | */ |
| 158 | private function week_day($wday){ |
| 159 | if($wday == 6) |
| 160 | return 0; |
| 161 | else |
| 162 | return ++$wday; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * bn_parsidate::persian_date() |
| 167 | * convert gregorian datetime to persian datetime |
| 168 | * |
| 169 | * @param mixed $format |
| 170 | * @param string $date |
| 171 | * @param string $lang |
| 172 | * @return datetime |
| 173 | */ |
| 174 | public function persian_date($format,$date='now',$lang='per') |
| 175 | { |
| 176 | $j_days_in_month = array(31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336, 365); |
| 177 | $timestamp =is_numeric($date) && (int)$date == $date?$date:strtotime($date); |
| 178 | |
| 179 | $date = getdate($timestamp); |
| 180 | list($date['year'],$date['mon'],$date['mday'])=self::gregorian_to_persian($date['year'],$date['mon'],$date['mday']); |
| 181 | $date['mon'] = (int)$date['mon']; |
| 182 | $date['mday'] = (int)$date['mday']; |
| 183 | $out = ''; |
| 184 | for($i=0;$i<strlen($format);$i++) |
| 185 | { |
| 186 | Switch($format[$i]) |
| 187 | { |
| 188 | //day |
| 189 | case 'd': |
| 190 | $out.=($date['mday']<10)?'0'.$date['mday']:$date['mday']; |
| 191 | break; |
| 192 | case 'D': |
| 193 | $out.=$this->persian_day_small[$date['wday']]; |
| 194 | break; |
| 195 | case'l': |
| 196 | $out.=$this->persian_day_names[$date['wday']]; |
| 197 | break; |
| 198 | case 'j': |
| 199 | $out.=$date['mday']; |
| 200 | break; |
| 201 | case'N': |
| 202 | $out.=$this->week_day($date['wday'])+1; |
| 203 | break; |
| 204 | case'w': |
| 205 | $out.=$this->week_day($date['wday']); |
| 206 | break; |
| 207 | case'z': |
| 208 | $out.=$this->j_days_in_month[$date['mon']]+$date['mday']; |
| 209 | break; |
| 210 | //week |
| 211 | case'W': |
| 212 | $yday=$this->j_days_sum_month[$date['mon']-1]+$date['mday']; |
| 213 | $out.=intval($yday/7); |
| 214 | break; |
| 215 | //month |
| 216 | case'f': |
| 217 | $mon=$date['mon']; |
| 218 | switch($mon) |
| 219 | { |
| 220 | case($mon<4): |
| 221 | $out.=$this->sesson[0]; |
| 222 | break; |
| 223 | case($mon<7): |
| 224 | $out.=$this->sesson[1]; |
| 225 | break; |
| 226 | case($mon<10): |
| 227 | $out.=$this->sesson[2]; |
| 228 | break; |
| 229 | case($mon>9): |
| 230 | $out.=$this->sesson[3]; |
| 231 | break; |
| 232 | } |
| 233 | break; |
| 234 | case'F': |
| 235 | $out.=$this->persian_month_names[(int)$date['mon']]; |
| 236 | break; |
| 237 | case'm': |
| 238 | $out.=($date['mon']<10)?'0'.$date['mon']:$date['mon']; |
| 239 | break; |
| 240 | case'M': |
| 241 | $out.=$this->persian_short_month_names[(int)$date['mon']]; |
| 242 | break; |
| 243 | case'n': |
| 244 | $out.=$date['mon']; |
| 245 | break; |
| 246 | case'S': |
| 247 | $out.='ا� |
| 248 | '; |
| 249 | break; |
| 250 | case't': |
| 251 | $out.=$this->j_days_in_month[(int)$date['mon']-1]; |
| 252 | break; |
| 253 | //year |
| 254 | case'L': |
| 255 | $out.=(($date['year']%4)==0)?1:0; |
| 256 | break; |
| 257 | case'o':case'Y': |
| 258 | $out.=$date['year']; |
| 259 | break; |
| 260 | case'y': |
| 261 | $out.=substr($date['year'],2,2); |
| 262 | break; |
| 263 | //time |
| 264 | case'a': |
| 265 | $out.=($date['hours']<12)?'ق.ظ':'ب.ظ'; |
| 266 | break; |
| 267 | case'A': |
| 268 | $out.=($date['hours']<12)?'قبل از ظهر':'بعد از ظهر'; |
| 269 | break; |
| 270 | case'B': |
| 271 | $out.=(int)(1+($date['mon']/3)); |
| 272 | break; |
| 273 | case'g': |
| 274 | $out.=($date['hours']>12)?$date['hours']-12:$date['hours']; |
| 275 | break; |
| 276 | case'G': |
| 277 | $out.=$date['hours']; |
| 278 | break; |
| 279 | case'h': |
| 280 | $hour=($date['hours']>12)?$date['hours']-12:$date['hours']; |
| 281 | $out.=($hour<10)?'0'.$hour:$hour; |
| 282 | break; |
| 283 | case'H': |
| 284 | $out.=($date['hours']<10)?'0'.$date['hours']:$date['hours']; |
| 285 | break; |
| 286 | case'i': |
| 287 | $out.=($date['minutes']<10)?'0'.$date['minutes']:$date['minutes']; |
| 288 | break; |
| 289 | case's': |
| 290 | $out.=($date['seconds']<10)?'0'.$date['seconds']:$date['seconds']; |
| 291 | break; |
| 292 | //full date time |
| 293 | case'c': |
| 294 | $out=$date['year'].'/'.$date['mon'].'/'.$date['mday'].' '.$date['hours'].':'.(($date['minutes']<10)?'0'.$date['minutes']:$date['minutes']).':'.(($date['seconds']<10)?'0'.$date['seconds']:$date['seconds']);//2004-02-12T15:19:21+00:00 |
| 295 | break; |
| 296 | case'r': |
| 297 | $out=$this->persian_day_names[$date['wday']].','.$date['mday'].' '.$this->persian_month_names[(int)$date['mon']].' '.$date['year'].' '.$date['hours'].':'.(($date['minutes']<10)?'0'.$date['minutes']:$date['minutes']).':'.(($date['seconds']<10)?'0'.$date['seconds']:$date['seconds']) ;//Thu, 21 Dec 2000 16:01:07 |
| 298 | break; |
| 299 | case'U': |
| 300 | $out=$timestamp; |
| 301 | break; |
| 302 | //others |
| 303 | case'e':case'I':case'i':case'O':case'P':case'T':case'Z':case'u':break; |
| 304 | default:$out.=$format[$i]; |
| 305 | } |
| 306 | } |
| 307 | if($lang=='per') |
| 308 | return self::trim_number($out); |
| 309 | else |
| 310 | return $out; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * bn_parsidate::gregurian_date() |
| 315 | * convert persian datetime to gregorian datetime |
| 316 | * @param mixed $format |
| 317 | * @param mixed $persiandate |
| 318 | * @return datetime |
| 319 | */ |
| 320 | public function gregurian_date($format,$persiandate) |
| 321 | { |
| 322 | preg_match_all('!\d+!', $persiandate, $matches); |
| 323 | $matches=$matches[0]; |
| 324 | list($year,$mon,$day)=self::persian_to_gregorian($matches[0],$matches[1],$matches[2]); |
| 325 | return date($format,mktime((isset($matches[3])?$matches[3]:0),(isset($matches[4])?$matches[4]:0),(isset($matches[5])?$matches[5]:0),$mon,$day,$year,-1)); |
| 326 | } |
| 327 | } |
| 328 | /* |
| 329 | * parsidate function |
| 330 | */ |
| 331 | /** |
| 332 | * parsidate() |
| 333 | * convert gregorian datetime to persian datetime |
| 334 | * |
| 335 | * @param mixed $input |
| 336 | * @param string $datetime |
| 337 | * @param string $lang |
| 338 | * @return datetime |
| 339 | */ |
| 340 | function parsidate($input,$datetime='now',$lang='per') |
| 341 | { |
| 342 | $bndate =bn_parsidate::getInstance(); |
| 343 | $bndate = $bndate->persian_date($input,$datetime,$lang); |
| 344 | return $bndate; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * gregdate() |
| 349 | * convert persian datetime to gregorian datetime |
| 350 | * |
| 351 | * @param mixed $input |
| 352 | * @param mixed $datetime |
| 353 | * @return datetime |
| 354 | */ |
| 355 | function gregdate($input,$datetime) |
| 356 | { |
| 357 | $bndate =bn_parsidate::getInstance(); |
| 358 | $bndate = $bndate->gregurian_date($input,$datetime); |
| 359 | return $bndate; |
| 360 | } |
| 361 |