بدون شک یکی از بهترین اسکریپتها برای کار با تاریخ شمسی، JDF هست که خیلیها ازش استفاده کردن. توی این تاپیک میخوام نحوه تبدیلش به کامپوننت Yii رو توضیح بدم.
ابتدا باید یک فایل به اسم JDF.php در مسیر protected/components ایجاد کنید و کد اسکریپت JDF رو که از لینک بالا دانلود کردین، داخلش قرار بدین. حالا یک کلاس به اسم JDF بسازین و اون رو از کلاس CComponent مشتق کنید. یک متد init با دسترسی public و بدون پارامتر ورودی هم داخل کلاس بسازین:
حالا یکی یکی توابع JDF رو بیارین داخل کلاس و هرجا توابع همدیگه رو مستقیم صدا زدن، اولشون <-this$ اضافه کنید. مثلاً اگه نوشته tr_num شما باید به $this->tr_num تبدیل کنید.
مرحله بعد، معرفی کامپوننت به برنامه است. کافیه توی فایل تنظیمات config/main.php در قسمت components این کد رو اضافه کنید:
حالا هرجا توی برنامه نیاز به این کامپوننت داشتین، با کمک Yii::app()->jdf بهش دسترسی پیدا میکنید. مثلاً اینطوری:
من برای اینکه یکم کارها راحتتر بشه و خوانایی کدها هم بیاد بالاتر، یک مقدار این کلاس رو دستکاری کردم و برای استفاده شما، کد آماده اون رو اینجا میگذارم:
که با این اوصاف، مثال قبلی رو با کلاسی که من نوشتم باید اینطوری صدا بزنید:
واضحه که اگه بخواین کارهای زیادی توی یک اسکریپت با این افزونه انجام بدین، هربار استفاده از Yii::app()->jdf کمی اذیت کننده است. خوب چرا اون رو توی یک متغیر نمیگذارین؟ ببینید:
از کدنویسی لذت ببرین.
هرگونه سؤال یا مشکلی بود، در خدمتم. توی همین تاپیک مطرح کنید.
ابتدا باید یک فایل به اسم JDF.php در مسیر protected/components ایجاد کنید و کد اسکریپت JDF رو که از لینک بالا دانلود کردین، داخلش قرار بدین. حالا یک کلاس به اسم JDF بسازین و اون رو از کلاس CComponent مشتق کنید. یک متد init با دسترسی public و بدون پارامتر ورودی هم داخل کلاس بسازین:
1 2 3 4 5 6 | class JDF extends CComponent { public function init() { } } |
حالا یکی یکی توابع JDF رو بیارین داخل کلاس و هرجا توابع همدیگه رو مستقیم صدا زدن، اولشون <-this$ اضافه کنید. مثلاً اگه نوشته tr_num شما باید به $this->tr_num تبدیل کنید.
مرحله بعد، معرفی کامپوننت به برنامه است. کافیه توی فایل تنظیمات config/main.php در قسمت components این کد رو اضافه کنید:
1 2 3 | 'jdf' => array ( 'class' => 'JDF' , ), |
حالا هرجا توی برنامه نیاز به این کامپوننت داشتین، با کمک Yii::app()->jdf بهش دسترسی پیدا میکنید. مثلاً اینطوری:
1 | echo Yii::app()->jdf->jdate( 'Y/m/d' ); |
من برای اینکه یکم کارها راحتتر بشه و خوانایی کدها هم بیاد بالاتر، یک مقدار این کلاس رو دستکاری کردم و برای استفاده شما، کد آماده اون رو اینجا میگذارم:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 | /* In The Name Of Allah */ /* Software Hijri_Shamsi , Solar(Jalali) Date and Time */ /* Copyright©2011, Reza Gholampanahi , http://jdf.scr.ir */ /* version 2.55 :: 1391/08/24 = 1433/12/18 = 2012/11/15 */ /* Converted to Yii component by MLF (admin@ncis.ir) */ /** * JDF Yii components */ class JDF extends CComponent { /** * @var string Current Time Zone */ public $timeZone ; /** * Initializes the component */ public function init() { $this ->timeZone = 'Asia/Tehran' ; mb_internal_encoding( 'utf-8' ); } /** * Set current timezone */ private function _setTimeZone() { if ( $this ->timeZone != 'local' ) { date_default_timezone_set( $this ->timeZone == '' ? 'Asia/Tehran' : $this ->timeZone); } } /** * Displays current Jalali date * @param string $format The date format * @param int $timeStamp Desired time stamp * @param string $language The translation language of numbers * @return string The requested date */ public function date ( $format , $timeStamp = '' , $language = 'fa' ) { $secondsCorrection = 0; // Server time correction, +/- seconds difference $this ->_setTimeZone(); $ts = $secondsCorrection + (( $timeStamp == '' or $timeStamp == 'now' ) ? time() : $this ->trNum( $timeStamp )); $date = explode ( '_' , date ( 'H_i_j_n_O_P_s_w_Y' , $ts )); list( $jalaliYear , $jalaliMonth , $jalaliDay ) = $this ->gregorianToJalali( $date [8], $date [3], $date [2]); $dayOfYear = ( $jalaliMonth < 7) ? (( $jalaliMonth - 1) * 31) + $jalaliDay - 1 : (( $jalaliMonth - 7) * 30) + $jalaliDay + 185; $leapYear = ( $this ->isLeapYear( $jalaliYear ) ? 1 : 0); $length = mb_strlen( $format ); $result = '' ; for ( $i = 0; $i < $length ; $i ++) { $currentChar = mb_substr( $format , $i , 1); if ( $currentChar == '\\' ) { $result .= mb_substr( $format , ++ $i , 1); continue ; } switch ( $currentChar ) { case 'E' : case 'R' : case 'x' : case 'X' : break ; case 'B' : case 'e' : case 'g' : case 'G' : case 'h' : case 'I' : case 'T' : case 'u' : case 'Z' : $result .= date ( $currentChar , $ts ); break ; case 'a' : $result .= ( $date [0] < 12) ? 'ق.ظ' : 'ب.ظ' ; break ; case 'A' : $result .= ( $date [0] < 12) ? 'قبل از ظهر' : 'بعد از ظهر' ; break ; case 'b' : $result .= (int)( $jalaliMonth / 3.1) + 1; break ; case 'c' : $result .= $jalaliYear . '/' . $jalaliMonth . '/' . $jalaliDay . ' ،' . $date [0] . ':' . $date [1] . ':' . $date [6] . ' ' . $date [5]; break ; case 'C' : $result .= (int)(( $jalaliYear + 99) / 100); break ; case 'd' : $result .= ( $jalaliDay < 10) ? '0' . $jalaliDay : $jalaliDay ; break ; case 'D' : $result .= $this ->dateWords( array ( 'kh' => $date [7]), ' ' ); break ; case 'f' : $result .= $this ->dateWords( array ( 'ff' => $jalaliMonth ), ' ' ); break ; case 'F' : $result .= $this ->dateWords( array ( 'mm' => $jalaliMonth ), ' ' ); break ; case 'H' : $result .= $date [0]; break ; case 'i' : $result .= $date [1]; break ; case 'j' : $result .= $jalaliDay ; break ; case 'J' : $result .= $this ->dateWords( array ( 'rr' => $jalaliDay ), ' ' ); break ; case 'k' ; $result .= $this ->trNum(100 - (int)( $dayOfYear / ( $leapYear + 365) * 1000) / 10, $language ); break ; case 'K' : $result .= $this ->trNum((int)( $dayOfYear / ( $leapYear + 365) * 1000) / 10, $language ); break ; case 'l' : $result .= $this ->dateWords( array ( 'rh' => $date [7]), ' ' ); break ; case 'L' : $result .= $leapYear ; break ; case 'm' : $result .= ( $jalaliMonth > 9) ? $jalaliMonth : '0' . $jalaliMonth ; break ; case 'M' : $result .= $this ->dateWords( array ( 'km' => $jalaliMonth ), ' ' ); break ; case 'n' : $result .= $jalaliMonth ; break ; case 'N' : $result .= $date [7] + 1; break ; case 'o' : $jalaliWeekDays = ( $date [7] == 6) ? 0 : $date [7] + 1; $daysOfNextYear = 364 + $leapYear - $dayOfYear ; $result .= ( $jalaliWeekDays > ( $dayOfYear + 3) && $dayOfYear < 3) ? $jalaliYear - 1 : (((3 - $daysOfNextYear ) > $jalaliWeekDays and $daysOfNextYear < 3) ? $jalaliYear + 1 : $jalaliYear ); break ; case 'O' : $result .= $date [4]; break ; case 'p' : $result .= $this ->dateWords( array ( 'mb' => $jalaliMonth ), ' ' ); break ; case 'P' : $result .= $date [5]; break ; case 'q' : $result .= $this ->dateWords( array ( 'sh' => $jalaliYear ), ' ' ); break ; case 'Q' : $result .= $leapYear + 364 - $dayOfYear ; break ; case 'r' : $key = $this ->dateWords( array ( 'rh' => $date [7], 'mm' => $jalaliMonth )); $result .= $date [0] . ':' . $date [1] . ':' . $date [6] . ' ' . $date [4] . ' ' . $key [ 'rh' ] . '، ' . $jalaliDay . ' ' . $key [ 'mm' ] . ' ' . $jalaliYear ; break ; case 's' : $result .= $date [6]; break ; case 'S' : $result .= 'ام' ; break ; case 't' : $result .= ( $jalaliMonth != 12) ? (31 - (int)( $jalaliMonth / 6.5)) : ( $leapYear + 29); break ; case 'U' : $result .= $ts ; break ; case 'v' : $result .= $this ->dateWords( array ( 'ss' => mb_substr( $jalaliYear , 2, 2)), ' ' ); break ; case 'V' : $result .= $this ->dateWords( array ( 'ss' => $jalaliYear ), ' ' ); break ; case 'w' : $result .= ( $date [7] == 6) ? 0 : $date [7] + 1; break ; case 'W' : $nextYearStartDay = (( $date [7] == 6) ? 0 : $date [7] + 1) - ( $dayOfYear % 7); if ( $nextYearStartDay < 0) { $nextYearStartDay += 7; } $weekNumber = (int)(( $dayOfYear + $nextYearStartDay ) / 7); if ( $nextYearStartDay < 4) { $weekNumber ++; } elseif ( $weekNumber < 1) { $weekNumber = ( $nextYearStartDay == 4 || $nextYearStartDay == (( $jalaliYear % 33 % 4 - 2 == (int)( $jalaliYear % 33 * .05)) ? 5 : 4)) ? 53 : 52; } $currenyYearEndDay = $nextYearStartDay + $leapYear ; if ( $currenyYearEndDay == 7) { $currenyYearEndDay = 0; } $result .= (( $leapYear + 363 - $dayOfYear ) < $currenyYearEndDay && $currenyYearEndDay < 3) ? '01' : (( $weekNumber < 10) ? '0' . $weekNumber : $weekNumber ); break ; case 'y' : $result .= mb_substr( $jalaliYear , 2, 2); break ; case 'Y' : $result .= $jalaliYear ; break ; case 'z' : $result .= $dayOfYear ; break ; default : $result .= $currentChar ; break ; } } return ( $language != 'en' ? $this ->trNum( $result , 'fa' , '.' ) : $result ); } /** * Converts timestamp to formatted string * @param string $format The string format * @param int $timeStamp Desired time stamp * @param string $language The translation language of numbers * @return string The formatted date */ public function strftime ( $format , $timeStamp = '' , $language = 'fa' ) { $secondsCorrection = 0; // Server time correction, +/- seconds difference $this ->_setTimeZone(); $ts = $secondsCorrection + (( $timeStamp == '' or $timeStamp == 'now' ) ? time() : $this ->trNum( $timeStamp )); $date = explode ( '_' , date ( 'h_H_i_j_n_s_w_Y' , $ts )); list( $jalaliYear , $jalaliMonth , $jalaliDay ) = $this ->gregorianToJalali( $date [7], $date [4], $date [3]); $daysOfYear = ( $jalaliMonth < 7) ? (( $jalaliMonth - 1) * 31) + $jalaliDay - 1 : (( $jalaliMonth - 7) * 30) + $jalaliDay + 185; $leapYear = $this ->isLeapYear( $jalaliYear ); $length = mb_strlen( $format ); $result = '' ; for ( $i = 0; $i < $length ; $i ++) { $currentChar = mb_substr( $format , $i , 1); if ( $currentChar == '%' ) { $currentChar = mb_substr( $format , ++ $i , 1); } else { $result .= $currentChar ; continue ; } switch ( $currentChar ) { /* Day */ case 'a' : $result .= $this ->dateWords( array ( 'kh' => $date [6]), ' ' ); break ; case 'A' : $result .= $this ->dateWords( array ( 'rh' => $date [6]), ' ' ); break ; case 'd' : $result .= ( $jalaliDay < 10 ? '0' . $jalaliDay : $jalaliDay ); break ; case 'e' : $result .= ( $jalaliDay < 10 ? ' ' . $jalaliDay : $jalaliDay ); break ; case 'j' : $result .= str_pad ( $daysOfYear + 1, 3, 0, STR_PAD_LEFT); break ; case 'u' : $result .= $date [6] + 1; break ; case 'w' : $result .= ( $date [6] == 6) ? 0 : $date [6] + 1; break ; case 'U' : $lastWeekDayOfYear = (( $date [6] < 5) ? $date [6] + 2 : $date [6] - 5) - ( $daysOfYear % 7); if ( $lastWeekDayOfYear < 0) { $lastWeekDayOfYear += 7; } $weekNumber = (int)(( $daysOfYear + $lastWeekDayOfYear ) / 7) + 1; if ( $lastWeekDayOfYear > 3 or $lastWeekDayOfYear == 1) $weekNumber --; $result .= ( $weekNumber < 10 ? '0' . $weekNumber : $weekNumber ); break ; case 'V' : $lastWeekDayOfYear = (( $date [6] == 6) ? 0 : $date [6] + 1) - ( $daysOfYear % 7); if ( $lastWeekDayOfYear < 0) { $lastWeekDayOfYear +=7; } $weekNumber = (int)(( $daysOfYear + $lastWeekDayOfYear ) / 7); if ( $lastWeekDayOfYear < 4) { $weekNumber ++; } elseif ( $weekNumber < 1) { $weekNumber = ( $lastWeekDayOfYear == 4 || $lastWeekDayOfYear == (( $jalaliYear % 33 % 4 - 2 == (int)( $jalaliYear % 33 * .05)) ? 5 : 4)) ? 53 : 52; } $lastWeekDay = $lastWeekDayOfYear + $leapYear ; if ( $lastWeekDay == 7) $lastWeekDay = 0; $result .=(( $leapYear + 363 - $daysOfYear ) < $lastWeekDay and $lastWeekDay < 3) ? '01' : (( $weekNumber < 10) ? '0' . $weekNumber : $weekNumber ); break ; case 'W' : $lastWeekDayOfYear = (( $date [6] == 6) ? 0 : $date [6] + 1) - ( $daysOfYear % 7); if ( $lastWeekDayOfYear < 0) { $lastWeekDayOfYear += 7; } $weekNumber = (int)(( $daysOfYear + $lastWeekDayOfYear ) / 7) + 1; if ( $lastWeekDayOfYear > 3) { $weekNumber --; } $result .= ( $weekNumber < 10) ? '0' . $weekNumber : $weekNumber ; break ; case 'b' : case 'h' : $result .= $this ->dateWords( array ( 'km' => $jalaliMonth ), ' ' ); break ; case 'B' : $result .= $this ->dateWords( array ( 'mm' => $jalaliMonth ), ' ' ); break ; case 'm' : $result .= ( $jalaliMonth > 9) ? $jalaliMonth : '0' . $jalaliMonth ; break ; case 'C' : $result .= mb_substr( $jalaliYear , 0, 2); break ; case 'g' : $jalaliWeekDay = ( $date [6] == 6) ? 0 : $date [6] + 1; $daysOfNextYear = 364 + $leapYear - $daysOfYear ; $result .= mb_substr(( $jalaliWeekDay > ( $daysOfYear + 3) && $daysOfYear < 3) ? $jalaliYear - 1 : (((3 - $daysOfNextYear ) > $jalaliWeekDay && $daysOfNextYear < 3) ? $jalaliYear + 1 : $jalaliYear ), 2, 2); break ; case 'G' : $jalaliWeekDay = ( $date [6] == 6) ? 0 : $date [6] + 1; $daysOfNextYear = 364 + $leapYear - $daysOfYear ; $result .= ( $jalaliWeekDay > ( $daysOfYear + 3) && $daysOfYear < 3) ? $jalaliYear - 1 : (((3 - $daysOfNextYear ) > $jalaliWeekDay && $daysOfNextYear < 3) ? $jalaliYear + 1 : $jalaliYear ); break ; case 'y' : $result .= mb_substr( $jalaliYear , 2, 2); break ; case 'Y' : $result .= $jalaliYear ; break ; case 'H' : $result .= $date [1]; break ; case 'I' : $result .= $date [0]; break ; case 'l' : $result .= ( $date [0] > 9) ? $date [0] : ' ' . (int) $date [0]; break ; case 'M' : $result .= $date [2]; break ; case 'p' : $result .= ( $date [1] < 12) ? 'قبل از ظهر' : 'بعد از ظهر' ; break ; case 'P' : $result .= ( $date [1] < 12) ? 'ق.ظ' : 'ب.ظ' ; break ; case 'r' : $result .= $date [0] . ':' . $date [2] . ':' . $date [5] . ' ' . (( $date [1] < 12) ? 'قبل از ظهر' : 'بعد از ظهر' ); break ; case 'R' : $result .= $date [1] . ':' . $date [2]; break ; case 'S' : $result .= $date [5]; break ; case 'T' : $result .= $date [1] . ':' . $date [2] . ':' . $date [5]; break ; case 'X' : $result .= $date [0] . ':' . $date [2] . ':' . $date [5]; break ; case 'z' : $result .= $this -> date ( 'O' , $ts ); break ; case 'Z' : $result .= $this -> date ( 'T' , $ts ); break ; case 'c' : $key = $this ->dateWords( array ( 'rh' => $date [6], 'mm' => $jalaliMonth )); $result .= $date [1] . ':' . $date [2] . ':' . $date [5] . ' ' . $this -> date ( 'P' , $ts ) . ' ' . $key [ 'rh' ] . '، ' . $jalaliDay . ' ' . $key [ 'mm' ] . ' ' . $jalaliYear ; break ; case 'D' : $result .= mb_substr( $jalaliYear , 2, 2) . '/' . ( $jalaliMonth > 9 ? $jalaliMonth : '0' . $jalaliMonth ) . '/' . ( $jalaliDay < 10 ? '0' . $jalaliDay : $jalaliDay ); break ; case 'F' : $result .= $jalaliYear . '-' . ( $jalaliMonth > 9 ? $jalaliMonth : '0' . $jalaliMonth ) . '-' . ( $jalaliDay < 10 ? '0' . $jalaliDay : $jalaliDay ); break ; case 's' : $result .= $ts ; break ; case 'x' : $result .= mb_substr( $jalaliYear , 2, 2) . '/' . ( $jalaliMonth > 9 ? $jalaliMonth : '0' . $jalaliMonth ) . '/' . ( $jalaliDay < 10 ? '0' . $jalaliDay : $jalaliDay ); break ; case 'n' : $result .= PHP_EOL; break ; case 't' : $result .= "t" ; break ; case '%' : $result .= '%' ; break ; default : $result .= $currentChar ; break ; } } return ( $language != 'en' ? $this ->trNum( $result , 'fa' , '.' ) : $result ); } /** * Makes timestamp from given jalali info * @param int $hour hour * @param int $minute minute * @param int $second second * @param int $jalaliMonth Jalali month * @param int $jajaliDay Jalali day * @param int $jalaliYear Jalali year * @param int|boolean $isDST Daylight saving time * @return int The calculated time stamp */ public function mktime ( $hour = '' , $minute = '' , $second = '' , $jalaliMonth = '' , $jajaliDay = '' , $jalaliYear = '' , $isDST = -1) { $hour = $this ->trNum( $hour ); $minute = $this ->trNum( $minute ); $second = $this ->trNum( $second ); $jalaliMonth = $this ->trNum( $jalaliMonth ); $jajaliDay = $this ->trNum( $jajaliDay ); $jalaliYear = $this ->trNum( $jalaliYear ); if ( $hour == '' && $minute == '' && $second == '' && $jalaliMonth == '' && $jajaliDay == '' && $jalaliYear == '' ) { return mktime (); } else { list( $year , $minute , $day ) = $this ->jalaliToGregorian( $jalaliYear , $jalaliMonth , $jajaliDay ); return mktime ( $hour , $minute , $second , $minute , $day , $year , $isDST ); } } /** * Gets the Jalali info of given timestamp * @param int $timeStamp The requested time stamp * @param string $trNum The translation language of numbers * @return array Jalali info as an array */ function getDate ( $timeStamp = '' , $trNum = 'en' ) { $ts = ( $timeStamp == '' ) ? time() : tr_num( $timeStamp ); $jDate = explode ( '_' , $this -> date ( 'F_G_i_j_l_n_s_w_Y_z' , $ts , '' , $this ->timeZone, $trNum )); return array ( 'seconds' => $this ->trNum((int) $this ->trNum( $jDate [6]), $trNum ), 'minutes' => $this ->trNum((int) $this ->trNum( $jDate [2]), $trNum ), 'hours' => $jDate [1], 'mday' => $jDate [3], 'wday' => $jDate [7], 'mon' => $jDate [5], 'year' => $jDate [8], 'yday' => $jDate [9], 'weekday' => $jDate [4], 'month' => $jDate [0], 0 => $this ->trNum( $ts , $trNum ) ); } /** * Checks to see whether the given date is valid or not * @param int $jalaliMonth Jalali month * @param int $jalaliDay Jalali day * @param int $jalaliYear Jalali year * @return boolean The check result of date validation */ public function checkDate ( $jalaliMonth , $jalaliDay , $jalaliYear ) { $jalaliMonth = $this ->trNum( $jalaliMonth ); $jalaliDay = $this ->trNum( $jalaliDay ); $jalaliYear = $this ->trNum( $jalaliYear ); $lastDayOfMonth = ( $jalaliMonth == 12) ? ( $this ->isLeapYear( $jalaliYear ) ? 30 : 29) : 31 - (int)( $jalaliMonth / 6.5); return ( $jalaliMonth > 0 && $jalaliDay > 0 && $jalaliYear > 0 && $jalaliMonth < 13 && $jalaliDay <= $lastDayOfMonth ); } /** * Converts numbers of a string to English of Farsi * @param string $string The string to convert * @param string $language Target language (en|fa) * @param string $digitSeparator The replacement character for digit separator * @return string The converted string */ public function trNum( $string , $language = 'en' , $digitSeparator = '٫' ) { $latinNumbers = array ( '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '.' ); $farsiNumbers = array ( '۰' , '۱' , '۲' , '۳' , '۴' , '۵' , '۶' , '۷' , '۸' , '۹' , $digitSeparator ); if ( $language == 'fa' ) { return str_replace ( $latinNumbers , $farsiNumbers , $string ); } return str_replace ( $farsiNumbers , $latinNumbers , $string ); } /** * Converts date to word equivalence * @param array $dateElements The date elements * @param string $glue The glue used to convert array to string * @return array|string The converted date as an string */ public function dateWords( $dateElements , $glue = '' ) { foreach ( $dateElements as $key => $value ) { $value = (int) $this ->trNum( $value ); switch ( $key ) { case 'ss' : $lastSecondChar = mb_substr( $value , -2, 1); $tens = $tensAndOnes = $ones = '' ; if ( $lastSecondChar == 1) { $separator = '' ; $tenToTwenty = array ( 'ده' , 'یازده' , 'دوازده' , 'سیزده' , 'چهارده' , 'پانزده' , 'شانزده' , 'هفده' , 'هجده' , 'نوزده' ); $tensAndOnes = $tenToTwenty [mb_substr( $value , -2, 2) - 10]; } else { $lastThirdChar = mb_substr( $value , -3, 1); $separator = ( $lastSecondChar == 0 or $lastThirdChar == 0) ? '' : ' و ' ; $tenWords = array ( '' , '' , 'بیست' , 'سی' , 'چهل' , 'پنجاه' , 'شصت' , 'هفتاد' , 'هشتاد' , 'نود' ); $tens = $tenWords [ $lastSecondChar ]; $oneWords = array ( '' , 'یک' , 'دو' , 'سه' , 'چهار' , 'پنج' , 'شش' , 'هفت' , 'هشت' , 'نه' ); $ones = $oneWords [ $lastThirdChar ]; } $yearNumbers = array ( '12' , '13' , '14' , '19' , '20' ); $yearWords = array ( 'هزار و دویست' , 'هزار و سیصد' , 'هزار و چهارصد' , 'هزار و نهصد' , 'دوهزار' ); $dateElements [ $key ] = (( $value > 99) ? str_ireplace ( $yearNumbers , $yearWords , mb_substr( $value , 0, 2)) . ((mb_substr( $value , 2, 2) == '00' ) ? '' : ' و ' ) : '' ) . $tenWords . $separator . $tensAndOnes . $ones ; break ; case 'mm' : $monthNames = array ( 'فروردین' , 'اردیبهشت' , 'خرداد' , 'تیر' , 'مرداد' , 'شهریور' , 'مهر' , 'آبان' , 'آذر' , 'دی' , 'بهمن' , 'اسفند' ); $dateElements [ $key ] = $monthNames [ $value - 1]; break ; case 'rr' : $dayWords = array ( 'یک' , 'دو' , 'سه' , 'چهار' , 'پنج' , 'شش' , 'هفت' , 'هشت' , 'نه' , 'ده' , 'یازده' , 'دوازده' , 'سیزده' , 'چهارده' , 'پانزده' , 'شانزده' , 'هفده' , 'هجده' , 'نوزده' , 'بیست' , 'بیست و یک' , 'بیست و دو' , 'بیست و سه' , 'بیست و چهار' , 'بیست و پنج' , 'بیست و شش' , 'بیست و هفت' , 'بیست و هشت' , 'بیست و نه' , 'سی' , 'سی و یک' ); $dateElements [ $key ] = $dayWords [ $value - 1]; break ; case 'rh' : $weekDays = array ( 'یکشنبه' , 'دوشنبه' , 'سه شنبه' , 'چهارشنبه' , 'پنجشنبه' , 'جمعه' , 'شنبه' ); $dateElements [ $key ] = $weekDays [ $value ]; break ; case 'sh' : $animalYears = array ( 'مار' , 'اسب' , 'گوسفند' , 'میمون' , 'مرغ' , 'سگ' , 'خوک' , 'موش' , 'گاو' , 'پلنگ' , 'خرگوش' , 'نهنگ' ); $dateElements [ $key ] = $animalYears [ $value % 12]; break ; case 'mb' : $ancientMonths = array ( 'حمل' , 'ثور' , 'جوزا' , 'سرطان' , 'اسد' , 'سنبله' , 'میزان' , 'عقرب' , 'قوس' , 'جدی' , 'دلو' , 'حوت' ); $dateElements [ $key ] = $ancientMonths [ $value - 1]; break ; case 'ff' : $seasons = array ( 'بهار' , 'تابستان' , 'پاییز' , 'زمستان' ); $dateElements [ $key ] = $seasons [ $value - 1]; break ; case 'km' : $briefMonths = array ( 'فر' , 'ار' , 'خر' , 'تی' , 'مر' , 'شه' , 'مه' , 'آب' , 'آذ' , 'دی' , 'به' , 'اس' ); $dateElements [ $key ] = $briefMonths [ $value - 1]; break ; case 'kh' : $briefDays = array ( 'ی' , 'د' , 'س' , 'چ' , 'پ' , 'ج' , 'ش' ); $dateElements [ $key ] = $briefMonths [ $value ]; break ; default : $dateElements [ $key ] = $value ; break ; } } return ( $glue == '' ? $dateElements : implode( $glue , $dateElements )); } /** * Converts Gregorian date to Jalali * @param int $gregorianYear Gregorian year * @param int $gregorialMonth Gregorian month * @param int $gregorianDay Gregorian day * @param string $glue The glue to convert the result to string * @return array|string The Jalali date */ function gregorianToJalali( $gregorianYear , $gregorialMonth , $gregorianDay , $glue = '' ) { $gregorianYear = $this ->trNum( $gregorianYear ); $gregorialMonth = $this ->trNum( $gregorialMonth ); $gregorianDay = $this ->trNum( $gregorianDay ); $yearReminderOfFour = $gregorianYear % 4; $elapsedDaysOfYear = array (0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334); $gregorianDaysOfYear = $elapsedDaysOfYear [(int) $gregorialMonth ] + $gregorianDay ; if ( $yearReminderOfFour == 0 && $gregorialMonth > 2) { $gregorianDaysOfYear ++; } $leapDay = (int)((( $gregorianYear - 16) % 132) * .0305); $after = ( $leapDay == 3 or $leapDay < ( $yearReminderOfFour - 1) or $yearReminderOfFour == 0) ? 286 : 287; $before = (( $leapDay == 1 or $leapDay == 2) and ( $leapDay == $yearReminderOfFour or $yearReminderOfFour == 1)) ? 78 : (( $leapDay == 3 and $yearReminderOfFour == 0) ? 80 : 79); if ((int)(( $gregorianYear - 10) / 63) == 30) { $after --; $before ++; } if ( $gregorianDaysOfYear > $before ) { $jalaliYear = $gregorianYear - 621; $jalaliDaysOfYear = $gregorianDaysOfYear - $before ; } else { $jalaliYear = $gregorianYear - 622; $jalaliDaysOfYear = $gregorianDaysOfYear + $after ; } if ( $jalaliDaysOfYear < 187) { $jalaliMonth = (int)(( $jalaliDaysOfYear - 1) / 31); $jalaliDay = $jalaliDaysOfYear - (31 * $jalaliMonth ++); } else { $jalaliMonth = (int)(( $jalaliDaysOfYear - 187) / 30); $jalaliDay = $jalaliDaysOfYear - 186 - ( $jalaliMonth * 30); $jalaliMonth += 7; } $result = array ( $jalaliYear , $jalaliMonth , $jalaliDay ); return ( $glue == '' ? $result : implode( $glue , $result )); } /** * Converts Jalali date to Gregorian * @param int $jalaliYear Jalali year * @param int $jalaliMonth Jalali month * @param int $jalaliDay Jalali day * @param string $glue The glue to convert the result to string * @return array|string The Gregorian date */ function jalaliToGregorian( $jalaliYear , $jalaliMonth , $jalaliDay , $glue = '' ) { $jalaliYear = $this ->trNum( $jalaliYear ); $jalaliMonth = $this ->trNum( $jalaliMonth ); $jalaliDay = $this ->trNum( $jalaliDay ); $yearRemindeOfFour = ( $jalaliYear + 1) % 4; $jalaliDaysOfYear = ( $jalaliMonth < 7) ? (( $jalaliMonth - 1) * 31) + $jalaliDay : (( $jalaliMonth - 7) * 30) + $jalaliDay + 186; $leapDay = (int)((( $jalaliYear - 55) % 132) * .0305); $after = ( $leapDay != 3 and $yearRemindeOfFour <= $leapDay ) ? 287 : 286; $before = (( $leapDay == 1 or $leapDay == 2) and ( $leapDay == $yearRemindeOfFour or $yearRemindeOfFour == 1)) ? 78 : (( $leapDay == 3 and $yearRemindeOfFour == 0) ? 80 : 79); if ((int)(( $jalaliYear - 19) / 63) == 20) { $after --; $before ++; } if ( $jalaliDaysOfYear <= $after ) { $gregorianYear = $jalaliYear + 621; $gregorianDay = $jalaliDaysOfYear + $before ; } else { $gregorianYear = $jalaliYear + 622; $gregorianDay = $jalaliDaysOfYear - $after ; } $gregorianDaysOfMonths = array (0, 31, ( $gregorianYear % 4 == 0) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); foreach ( $gregorianDaysOfMonths as $gregorianMonth => $daysOfMonth ) { if ( $gregorianDay <= $daysOfMonth ) { break ; } $gregorianDay -= $daysOfMonth ; } $result = array ( $gregorianYear , $gregorianMonth , $gregorianDay ); return ( $glue == '' ? $result : implode( $glue , $result )); } /** * Checks to see whether the given Jalali year is leap year * @param int $jalaliYear Jalali year * @return boolean The result of leap year check */ public function isLeapYear( $jalaliYear ) { return $jalaliYear % 33 % 4 - 1 == (int)( $jalaliYear % 33 * .05); } } |
که با این اوصاف، مثال قبلی رو با کلاسی که من نوشتم باید اینطوری صدا بزنید:
1 | Yii::app()->jdf-> date ( 'Y/m/d' ); |
واضحه که اگه بخواین کارهای زیادی توی یک اسکریپت با این افزونه انجام بدین، هربار استفاده از Yii::app()->jdf کمی اذیت کننده است. خوب چرا اون رو توی یک متغیر نمیگذارین؟ ببینید:
1 2 3 | $jdf = Yii::app()->jdf; echo $jdf -> date ( 'Y/m/d' ); echo $jdf ->trNum( $user ->credit, 'fa' ); |
از کدنویسی لذت ببرین.
هرگونه سؤال یا مشکلی بود، در خدمتم. توی همین تاپیک مطرح کنید.