Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CakeTime often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CakeTime, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class CakeTime { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The format to use when formatting a time using `CakeTime::nice()` |
||
| 33 | * |
||
| 34 | * The format should use the locale strings as defined in the PHP docs under |
||
| 35 | * `strftime` (http://php.net/manual/en/function.strftime.php) |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | * @see CakeTime::format() |
||
| 39 | */ |
||
| 40 | public static $niceFormat = '%a, %b %eS %Y, %H:%M'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The format to use when formatting a time using `CakeTime::timeAgoInWords()` |
||
| 44 | * and the difference is more than `CakeTime::$wordEnd` |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | * @see CakeTime::timeAgoInWords() |
||
| 48 | */ |
||
| 49 | public static $wordFormat = 'j/n/y'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The format to use when formatting a time using `CakeTime::niceShort()` |
||
| 53 | * and the difference is between 3 and 7 days |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | * @see CakeTime::niceShort() |
||
| 57 | */ |
||
| 58 | public static $niceShortFormat = '%B %d, %H:%M'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The format to use when formatting a time using `CakeTime::timeAgoInWords()` |
||
| 62 | * and the difference is less than `CakeTime::$wordEnd` |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | * @see CakeTime::timeAgoInWords() |
||
| 66 | */ |
||
| 67 | public static $wordAccuracy = array( |
||
| 68 | 'year' => "day", |
||
| 69 | 'month' => "day", |
||
| 70 | 'week' => "day", |
||
| 71 | 'day' => "hour", |
||
| 72 | 'hour' => "minute", |
||
| 73 | 'minute' => "minute", |
||
| 74 | 'second' => "second", |
||
| 75 | ); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The end of relative time telling |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | * @see CakeTime::timeAgoInWords() |
||
| 82 | */ |
||
| 83 | public static $wordEnd = '+1 month'; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Temporary variable containing the timestamp value, used internally in convertSpecifiers() |
||
| 87 | * |
||
| 88 | * @var integer |
||
| 89 | */ |
||
| 90 | protected static $_time = null; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Magic set method for backwards compatibility. |
||
| 94 | * Used by TimeHelper to modify static variables in CakeTime |
||
| 95 | * |
||
| 96 | * @param string $name Variable name |
||
| 97 | * @param mixes $value Variable value |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | public function __set($name, $value) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Magic set method for backwards compatibility. |
||
| 110 | * Used by TimeHelper to get static variables in CakeTime |
||
| 111 | * |
||
| 112 | * @param string $name Variable name |
||
| 113 | * @return mixed |
||
| 114 | */ |
||
| 115 | public function __get($name) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Converts a string representing the format for the function strftime and returns a |
||
| 126 | * windows safe and i18n aware format. |
||
| 127 | * |
||
| 128 | * @param string $format Format with specifiers for strftime function. |
||
| 129 | * Accepts the special specifier %S which mimics the modifier S for date() |
||
| 130 | * @param string $time UNIX timestamp |
||
| 131 | * @return string windows safe and date() function compatible format for strftime |
||
| 132 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::convertSpecifiers |
||
| 133 | */ |
||
| 134 | public static function convertSpecifiers($format, $time = null) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Auxiliary function to translate a matched specifier element from a regular expression into |
||
| 144 | * a windows safe and i18n aware specifier |
||
| 145 | * |
||
| 146 | * @param array $specifier match from regular expression |
||
| 147 | * @return string converted element |
||
| 148 | */ |
||
| 149 | protected static function _translateSpecifier($specifier) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Converts given time (in server's time zone) to user's local time, given his/her timezone. |
||
| 241 | * |
||
| 242 | * @param string $serverTime UNIX timestamp |
||
| 243 | * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object |
||
| 244 | * @return integer UNIX timestamp |
||
| 245 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::convert |
||
| 246 | */ |
||
| 247 | public static function convert($serverTime, $timezone) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Returns a timezone object from a string or the user's timezone object |
||
| 266 | * |
||
| 267 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 268 | * If null it tries to get timezone from 'Config.timezone' config var |
||
| 269 | * @return DateTimeZone Timezone object |
||
| 270 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::timezone |
||
| 271 | */ |
||
| 272 | public static function timezone($timezone = null) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Returns server's offset from GMT in seconds. |
||
| 297 | * |
||
| 298 | * @return integer Offset |
||
| 299 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::serverOffset |
||
| 300 | */ |
||
| 301 | public static function serverOffset() { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string. |
||
| 307 | * |
||
| 308 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 309 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 310 | * @return string Parsed timestamp |
||
| 311 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::fromString |
||
| 312 | */ |
||
| 313 | public static function fromString($dateString, $timezone = null) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Returns a nicely formatted date string for given Datetime string. |
||
| 354 | * |
||
| 355 | * See http://php.net/manual/en/function.strftime.php for information on formatting |
||
| 356 | * using locale strings. |
||
| 357 | * |
||
| 358 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 359 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 360 | * @param string $format The format to use. If null, `TimeHelper::$niceFormat` is used |
||
| 361 | * @return string Formatted date string |
||
| 362 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::nice |
||
| 363 | */ |
||
| 364 | public static function nice($dateString = null, $timezone = null, $format = null) { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Returns a formatted descriptive date string for given datetime string. |
||
| 378 | * |
||
| 379 | * If the given date is today, the returned string could be "Today, 16:54". |
||
| 380 | * If the given date is tomorrow, the returned string could be "Tomorrow, 16:54". |
||
| 381 | * If the given date was yesterday, the returned string could be "Yesterday, 16:54". |
||
| 382 | * If the given date is within next or last week, the returned string could be "On Thursday, 16:54". |
||
| 383 | * If $dateString's year is the current year, the returned string does not |
||
| 384 | * include mention of the year. |
||
| 385 | * |
||
| 386 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 387 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 388 | * @return string Described, relative date string |
||
| 389 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::niceShort |
||
| 390 | */ |
||
| 391 | public static function niceShort($dateString = null, $timezone = null) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Returns a partial SQL string to search for all records between two dates. |
||
| 433 | * |
||
| 434 | * @param integer|string|DateTime $begin UNIX timestamp, strtotime() valid string or DateTime object |
||
| 435 | * @param integer|string|DateTime $end UNIX timestamp, strtotime() valid string or DateTime object |
||
| 436 | * @param string $fieldName Name of database field to compare with |
||
| 437 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 438 | * @return string Partial SQL string. |
||
| 439 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::daysAsSql |
||
| 440 | */ |
||
| 441 | public static function daysAsSql($begin, $end, $fieldName, $timezone = null) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Returns a partial SQL string to search for all records between two times |
||
| 452 | * occurring on the same day. |
||
| 453 | * |
||
| 454 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 455 | * @param string $fieldName Name of database field to compare with |
||
| 456 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 457 | * @return string Partial SQL string. |
||
| 458 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::dayAsSql |
||
| 459 | */ |
||
| 460 | public static function dayAsSql($dateString, $fieldName, $timezone = null) { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Returns true if given datetime string is today. |
||
| 466 | * |
||
| 467 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 468 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 469 | * @return boolean True if datetime string is today |
||
| 470 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::isToday |
||
| 471 | */ |
||
| 472 | public static function isToday($dateString, $timezone = null) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Returns true if given datetime string is in the future. |
||
| 480 | * |
||
| 481 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 482 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 483 | * @return boolean True if datetime string is in the future |
||
| 484 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::isFuture |
||
| 485 | */ |
||
| 486 | public static function isFuture($dateString, $timezone = null) { |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Returns true if given datetime string is in the past. |
||
| 493 | * |
||
| 494 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 495 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 496 | * @return boolean True if datetime string is in the past |
||
| 497 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::isPast |
||
| 498 | */ |
||
| 499 | public static function isPast($dateString, $timezone = null) { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Returns true if given datetime string is within this week. |
||
| 506 | * |
||
| 507 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 508 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 509 | * @return boolean True if datetime string is within current week |
||
| 510 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::isThisWeek |
||
| 511 | */ |
||
| 512 | public static function isThisWeek($dateString, $timezone = null) { |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Returns true if given datetime string is within this month |
||
| 520 | * |
||
| 521 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 522 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 523 | * @return boolean True if datetime string is within current month |
||
| 524 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::isThisMonth |
||
| 525 | */ |
||
| 526 | public static function isThisMonth($dateString, $timezone = null) { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Returns true if given datetime string is within current year. |
||
| 534 | * |
||
| 535 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 536 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 537 | * @return boolean True if datetime string is within current year |
||
| 538 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::isThisYear |
||
| 539 | */ |
||
| 540 | public static function isThisYear($dateString, $timezone = null) { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Returns true if given datetime string was yesterday. |
||
| 548 | * |
||
| 549 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 550 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 551 | * @return boolean True if datetime string was yesterday |
||
| 552 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::wasYesterday |
||
| 553 | */ |
||
| 554 | public static function wasYesterday($dateString, $timezone = null) { |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Returns true if given datetime string is tomorrow. |
||
| 562 | * |
||
| 563 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 564 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 565 | * @return boolean True if datetime string was yesterday |
||
| 566 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::isTomorrow |
||
| 567 | */ |
||
| 568 | public static function isTomorrow($dateString, $timezone = null) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Returns the quarter |
||
| 576 | * |
||
| 577 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 578 | * @param boolean $range if true returns a range in Y-m-d format |
||
| 579 | * @return mixed 1, 2, 3, or 4 quarter of year or array if $range true |
||
| 580 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::toQuarter |
||
| 581 | */ |
||
| 582 | public static function toQuarter($dateString, $range = false) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime(). |
||
| 604 | * |
||
| 605 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 606 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 607 | * @return integer Unix timestamp |
||
| 608 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::toUnix |
||
| 609 | */ |
||
| 610 | public static function toUnix($dateString, $timezone = null) { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Returns a formatted date in server's timezone. |
||
| 616 | * |
||
| 617 | * If a DateTime object is given or the dateString has a timezone |
||
| 618 | * segment, the timezone parameter will be ignored. |
||
| 619 | * |
||
| 620 | * If no timezone parameter is given and no DateTime object, the passed $dateString will be |
||
| 621 | * considered to be in the UTC timezone. |
||
| 622 | * |
||
| 623 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 624 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 625 | * @param string $format date format string |
||
| 626 | * @return mixed Formatted date |
||
| 627 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::toServer |
||
| 628 | */ |
||
| 629 | public static function toServer($dateString, $timezone = null, $format = 'Y-m-d H:i:s') { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Returns a date formatted for Atom RSS feeds. |
||
| 655 | * |
||
| 656 | * @param string $dateString Datetime string or Unix timestamp |
||
| 657 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 658 | * @return string Formatted date string |
||
| 659 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::toAtom |
||
| 660 | */ |
||
| 661 | public static function toAtom($dateString, $timezone = null) { |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Formats date for RSS feeds |
||
| 667 | * |
||
| 668 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 669 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 670 | * @return string Formatted date string |
||
| 671 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::toRSS |
||
| 672 | */ |
||
| 673 | public static function toRSS($dateString, $timezone = null) { |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Returns either a relative or a formatted absolute date depending |
||
| 701 | * on the difference between the current time and given datetime. |
||
| 702 | * $datetime should be in a *strtotime* - parsable format, like MySQL's datetime datatype. |
||
| 703 | * |
||
| 704 | * ### Options: |
||
| 705 | * |
||
| 706 | * - `format` => a fall back format if the relative time is longer than the duration specified by end |
||
| 707 | * - `accuracy` => Specifies how accurate the date should be described (array) |
||
| 708 | * - year => The format if years > 0 (default "day") |
||
| 709 | * - month => The format if months > 0 (default "day") |
||
| 710 | * - week => The format if weeks > 0 (default "day") |
||
| 711 | * - day => The format if weeks > 0 (default "hour") |
||
| 712 | * - hour => The format if hours > 0 (default "minute") |
||
| 713 | * - minute => The format if minutes > 0 (default "minute") |
||
| 714 | * - second => The format if seconds > 0 (default "second") |
||
| 715 | * - `end` => The end of relative time telling |
||
| 716 | * - `relativeString` => The printf compatible string when outputting relative time |
||
| 717 | * - `absoluteString` => The printf compatible string when outputting absolute time |
||
| 718 | * - `userOffset` => Users offset from GMT (in hours) *Deprecated* use timezone intead. |
||
| 719 | * - `timezone` => The user timezone the timestamp should be formatted in. |
||
| 720 | * |
||
| 721 | * Relative dates look something like this: |
||
| 722 | * |
||
| 723 | * - 3 weeks, 4 days ago |
||
| 724 | * - 15 seconds ago |
||
| 725 | * |
||
| 726 | * Default date formatting is d/m/yy e.g: on 18/2/09 |
||
| 727 | * |
||
| 728 | * The returned string includes 'ago' or 'on' and assumes you'll properly add a word |
||
| 729 | * like 'Posted ' before the function output. |
||
| 730 | * |
||
| 731 | * NOTE: If the difference is one week or more, the lowest level of accuracy is day |
||
| 732 | * |
||
| 733 | * @param integer|string|DateTime $dateTime Datetime UNIX timestamp, strtotime() valid string or DateTime object |
||
| 734 | * @param array $options Default format if timestamp is used in $dateString |
||
| 735 | * @return string Relative time string. |
||
| 736 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::timeAgoInWords |
||
| 737 | */ |
||
| 738 | public static function timeAgoInWords($dateTime, $options = array()) { |
||
| 938 | |||
| 939 | /** |
||
| 940 | * Returns true if specified datetime was within the interval specified, else false. |
||
| 941 | * |
||
| 942 | * @param string|integer $timeInterval the numeric value with space then time type. |
||
| 943 | * Example of valid types: 6 hours, 2 days, 1 minute. |
||
| 944 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 945 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 946 | * @return boolean |
||
| 947 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::wasWithinLast |
||
| 948 | */ |
||
| 949 | View Code Duplication | public static function wasWithinLast($timeInterval, $dateString, $timezone = null) { |
|
| 961 | |||
| 962 | /** |
||
| 963 | * Returns true if specified datetime is within the interval specified, else false. |
||
| 964 | * |
||
| 965 | * @param string|integer $timeInterval the numeric value with space then time type. |
||
| 966 | * Example of valid types: 6 hours, 2 days, 1 minute. |
||
| 967 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 968 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 969 | * @return boolean |
||
| 970 | */ |
||
| 971 | View Code Duplication | public static function isWithinNext($timeInterval, $dateString, $timezone = null) { |
|
| 983 | |||
| 984 | /** |
||
| 985 | * Returns gmt as a UNIX timestamp. |
||
| 986 | * |
||
| 987 | * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object |
||
| 988 | * @return integer UNIX timestamp |
||
| 989 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::gmt |
||
| 990 | */ |
||
| 991 | public static function gmt($dateString = null) { |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string. |
||
| 1008 | * This function also accepts a time string and a format string as first and second parameters. |
||
| 1009 | * In that case this function behaves as a wrapper for TimeHelper::i18nFormat() |
||
| 1010 | * |
||
| 1011 | * ## Examples |
||
| 1012 | * |
||
| 1013 | * Create localized & formatted time: |
||
| 1014 | * |
||
| 1015 | * {{{ |
||
| 1016 | * CakeTime::format('2012-02-15', '%m-%d-%Y'); // returns 02-15-2012 |
||
| 1017 | * CakeTime::format('2012-02-15 23:01:01', '%c'); // returns preferred date and time based on configured locale |
||
| 1018 | * CakeTime::format('0000-00-00', '%d-%m-%Y', 'N/A'); // return N/A becuase an invalid date was passed |
||
| 1019 | * CakeTime::format('2012-02-15 23:01:01', '%c', 'N/A', 'America/New_York'); // converts passed date to timezone |
||
| 1020 | * }}} |
||
| 1021 | * |
||
| 1022 | * @param integer|string|DateTime $date UNIX timestamp, strtotime() valid string or DateTime object (or a date format string) |
||
| 1023 | * @param integer|string|DateTime $format date format string (or UNIX timestamp, strtotime() valid string or DateTime object) |
||
| 1024 | * @param boolean|string $default if an invalid date is passed it will output supplied default value. Pass false if you want raw conversion value |
||
| 1025 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 1026 | * @return string Formatted date string |
||
| 1027 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::format |
||
| 1028 | * @see CakeTime::i18nFormat() |
||
| 1029 | */ |
||
| 1030 | public static function format($date, $format = null, $default = false, $timezone = null) { |
||
| 1039 | |||
| 1040 | /** |
||
| 1041 | * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string. |
||
| 1042 | * It takes into account the default date format for the current language if a LC_TIME file is used. |
||
| 1043 | * |
||
| 1044 | * @param integer|string|DateTime $date UNIX timestamp, strtotime() valid string or DateTime object |
||
| 1045 | * @param string $format strftime format string. |
||
| 1046 | * @param boolean|string $default if an invalid date is passed it will output supplied default value. Pass false if you want raw conversion value |
||
| 1047 | * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object |
||
| 1048 | * @return string Formatted and translated date string |
||
| 1049 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::i18nFormat |
||
| 1050 | */ |
||
| 1051 | public static function i18nFormat($date, $format = null, $default = false, $timezone = null) { |
||
| 1061 | |||
| 1062 | /** |
||
| 1063 | * Get list of timezone identifiers |
||
| 1064 | * |
||
| 1065 | * @param integer|string $filter A regex to filter identifer |
||
| 1066 | * Or one of DateTimeZone class constants (PHP 5.3 and above) |
||
| 1067 | * @param string $country A two-letter ISO 3166-1 compatible country code. |
||
| 1068 | * This option is only used when $filter is set to DateTimeZone::PER_COUNTRY (available only in PHP 5.3 and above) |
||
| 1069 | * @param boolean $group If true (default value) groups the identifiers list by primary region |
||
| 1070 | * @return array List of timezone identifiers |
||
| 1071 | * @since 2.2 |
||
| 1072 | * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::listTimezones |
||
| 1073 | */ |
||
| 1074 | public static function listTimezones($filter = null, $country = null, $group = true) { |
||
| 1114 | |||
| 1115 | /** |
||
| 1116 | * Multibyte wrapper for strftime. |
||
| 1117 | * |
||
| 1118 | * Handles utf8_encoding the result of strftime when necessary. |
||
| 1119 | * |
||
| 1120 | * @param string $format Format string. |
||
| 1121 | * @param integer $date Timestamp to format. |
||
| 1122 | * @return string formatted string with correct encoding. |
||
| 1123 | */ |
||
| 1124 | protected static function _strftime($format, $date) { |
||
| 1140 | |||
| 1141 | } |
||
| 1142 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: