PageRenderTime 65ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/symfony/lib/i18n/sfDateFormat.class.php

https://bitbucket.org/ealexandru/jobeet
PHP | 817 lines | 501 code | 64 blank | 252 comment | 79 complexity | 6dee3a849f084278fd0e08e012bf41d0 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /**
  3. * sfDateFormat class file.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the BSD License.
  7. *
  8. * Copyright(c) 2004 by Qiang Xue. All rights reserved.
  9. *
  10. * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
  11. * The latest version of PRADO can be obtained from:
  12. * {@link http://prado.sourceforge.net/}
  13. *
  14. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  15. * @version $Id: sfDateFormat.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
  16. * @package symfony
  17. * @subpackage i18n
  18. */
  19. /**
  20. * sfDateFormat class.
  21. *
  22. * The sfDateFormat class allows you to format dates and times with
  23. * predefined styles in a locale-sensitive manner. Formatting times
  24. * with the sfDateFormat class is similar to formatting dates.
  25. *
  26. * Formatting dates with the sfDateFormat class is a two-step process.
  27. * First, you create a formatter with the getDateInstance method.
  28. * Second, you invoke the format method, which returns a string containing
  29. * the formatted date.
  30. *
  31. * DateTime values are formatted using standard or custom patterns stored
  32. * in the properties of a DateTimeFormatInfo.
  33. *
  34. * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
  35. * @version v1.0, last update on Sat Dec 04 14:10:49 EST 2004
  36. * @package symfony
  37. * @subpackage i18n
  38. */
  39. class sfDateFormat
  40. {
  41. /**
  42. * A list of tokens and their function call.
  43. * @var array
  44. */
  45. protected $tokens = array(
  46. 'G' => 'Era',
  47. 'y' => 'year',
  48. 'M' => 'mon',
  49. 'd' => 'mday',
  50. 'h' => 'Hour12',
  51. 'H' => 'hours',
  52. 'm' => 'minutes',
  53. 's' => 'seconds',
  54. 'E' => 'wday',
  55. 'D' => 'yday',
  56. 'F' => 'DayInMonth',
  57. 'w' => 'WeekInYear',
  58. 'W' => 'WeekInMonth',
  59. 'a' => 'AMPM',
  60. 'k' => 'HourInDay',
  61. 'K' => 'HourInAMPM',
  62. 'z' => 'TimeZone'
  63. );
  64. /**
  65. * A list of methods, to be used by the token function calls.
  66. * @var array
  67. */
  68. protected $methods = array();
  69. /**
  70. * The sfDateTimeFormatInfo, containing culture specific patterns and names.
  71. * @var sfDateTimeFormatInfo
  72. */
  73. protected $formatInfo;
  74. /**
  75. * Initializes a new sfDateFormat.
  76. *
  77. * @param mixed $formatInfo either, null, a sfCultureInfo instance, a DateTimeFormatInfo instance, or a locale.
  78. * @return sfDateFormat instance
  79. */
  80. function __construct($formatInfo = null)
  81. {
  82. if (null === $formatInfo)
  83. {
  84. $this->formatInfo = sfDateTimeFormatInfo::getInvariantInfo();
  85. }
  86. else if ($formatInfo instanceof sfCultureInfo)
  87. {
  88. $this->formatInfo = $formatInfo->DateTimeFormat;
  89. }
  90. else if ($formatInfo instanceof sfDateTimeFormatInfo)
  91. {
  92. $this->formatInfo = $formatInfo;
  93. }
  94. else
  95. {
  96. $this->formatInfo = sfDateTimeFormatInfo::getInstance($formatInfo);
  97. }
  98. $this->methods = get_class_methods($this);
  99. }
  100. /**
  101. * Guesses a date without calling strtotime.
  102. *
  103. * @author Olivier Verdier <Olivier.Verdier@gmail.com>
  104. * @param mixed $time the time as integer or string in strtotime format.
  105. * @param string $pattern the input pattern; default is sql date or timestamp
  106. * @return array same array as the getdate function
  107. */
  108. public function getDate($time, $pattern = null)
  109. {
  110. if (null === $time)
  111. {
  112. return null;
  113. }
  114. // if the type is not a php timestamp
  115. $isString = (string) $time !== (string) (int) $time;
  116. if ($isString)
  117. {
  118. if (!$pattern)
  119. {
  120. if (strlen($time) == 10)
  121. {
  122. $pattern = 'i';
  123. }
  124. else // otherwise, default:
  125. {
  126. $pattern = 'I';
  127. }
  128. }
  129. $pattern = $this->getPattern($pattern);
  130. $tokens = $this->getTokens($pattern);
  131. $pregPattern = '';
  132. $matchNames = array();
  133. // current regex allows any char at the end. avoids duplicating [^\d]+ pattern
  134. // this could cause issues with utf character width
  135. $allowsAllChars=true;
  136. foreach ($tokens as $token)
  137. {
  138. if ($matchName = $this->getFunctionName($token))
  139. {
  140. $allowsAllChars = false;
  141. $pregPattern .= '(\d+)';
  142. $matchNames[] = $matchName;
  143. }
  144. else
  145. {
  146. if (!$allowsAllChars)
  147. {
  148. $allowsAllChars = true;
  149. $pregPattern .= '[^\d]+';
  150. }
  151. }
  152. }
  153. preg_match('@'.$pregPattern.'@', $time, $matches);
  154. array_shift($matches);
  155. if (count($matchNames) == count($matches))
  156. {
  157. $date = array_combine($matchNames, $matches);
  158. // guess the date if input with two digits
  159. if (strlen($date['year']) == 2)
  160. {
  161. $date['year'] = date('Y', mktime(0, 0, 0, 1, 1, $date['year']));
  162. }
  163. $date = array_map('intval', $date);
  164. }
  165. }
  166. // the last attempt has failed we fall back on the default method
  167. if (!isset($date))
  168. {
  169. if ($isString)
  170. {
  171. $numericalTime = @strtotime($time);
  172. if ($numericalTime === false)
  173. {
  174. throw new sfException(sprintf('Impossible to parse date "%s" with format "%s".', $time, $pattern));
  175. }
  176. }
  177. else
  178. {
  179. $numericalTime = $time;
  180. }
  181. $date = @getdate($numericalTime);
  182. }
  183. // we set default values for the time
  184. foreach (array('hours', 'minutes', 'seconds') as $timeDiv)
  185. {
  186. if (!isset($date[$timeDiv]))
  187. {
  188. $date[$timeDiv] = 0;
  189. }
  190. }
  191. return $date;
  192. }
  193. /**
  194. * Formats a date according to the pattern.
  195. *
  196. * @param mixed $time the time as integer or string in strtotime format.
  197. * @param string $pattern the pattern
  198. * @param string $inputPattern the input pattern
  199. * @param string $charset the charset
  200. * @return string formatted date time.
  201. */
  202. public function format($time, $pattern = 'F', $inputPattern = null, $charset = 'UTF-8')
  203. {
  204. $date = $this->getDate($time, $inputPattern);
  205. if (null === $pattern)
  206. {
  207. $pattern = 'F';
  208. }
  209. $pattern = $this->getPattern($pattern);
  210. $tokens = $this->getTokens($pattern);
  211. for ($i = 0, $max = count($tokens); $i < $max; $i++)
  212. {
  213. $pattern = $tokens[$i];
  214. if ($pattern{0} == "'" && $pattern{strlen($pattern) - 1} == "'")
  215. {
  216. $tokens[$i] = str_replace('``````', '\'', preg_replace('/(^\')|(\'$)/', '', $pattern));
  217. }
  218. else if ($pattern == '``````')
  219. {
  220. $tokens[$i] = '\'';
  221. }
  222. else
  223. {
  224. $function = ucfirst($this->getFunctionName($pattern));
  225. if ($function != null)
  226. {
  227. $fName = 'get'.$function;
  228. if (in_array($fName, $this->methods))
  229. {
  230. $tokens[$i] = $this->$fName($date, $pattern);
  231. }
  232. else
  233. {
  234. throw new sfException(sprintf('Function %s not found.', $function));
  235. }
  236. }
  237. }
  238. }
  239. return sfToolkit::I18N_toEncoding(implode('', $tokens), $charset);
  240. }
  241. /**
  242. * For a particular token, get the corresponding function to call.
  243. *
  244. * @param string $token token
  245. * @return mixed the function if good token, null otherwise.
  246. */
  247. protected function getFunctionName($token)
  248. {
  249. if (isset($this->tokens[$token{0}]))
  250. {
  251. return $this->tokens[$token{0}];
  252. }
  253. }
  254. /**
  255. * Gets the pattern from DateTimeFormatInfo or some predefined patterns.
  256. * If the $pattern parameter is an array of 2 element, it will assume
  257. * that the first element is the date, and second the time
  258. * and try to find an appropriate pattern and apply
  259. * DateTimeFormatInfo::formatDateTime
  260. * See the tutorial documentation for futher details on the patterns.
  261. *
  262. * @param mixed $pattern a pattern.
  263. * @return string a pattern.
  264. * @see DateTimeFormatInfo::formatDateTime()
  265. */
  266. public function getPattern($pattern)
  267. {
  268. if (is_array($pattern) && count($pattern) == 2)
  269. {
  270. return $this->formatInfo->formatDateTime($this->getPattern($pattern[0]), $this->getPattern($pattern[1]));
  271. }
  272. switch ($pattern)
  273. {
  274. case 'd':
  275. return $this->formatInfo->ShortDatePattern;
  276. break;
  277. case 'D':
  278. return $this->formatInfo->LongDatePattern;
  279. break;
  280. case 'p':
  281. return $this->formatInfo->MediumDatePattern;
  282. break;
  283. case 'P':
  284. return $this->formatInfo->FullDatePattern;
  285. break;
  286. case 't':
  287. return $this->formatInfo->ShortTimePattern;
  288. break;
  289. case 'T':
  290. return $this->formatInfo->LongTimePattern;
  291. break;
  292. case 'q':
  293. return $this->formatInfo->MediumTimePattern;
  294. break;
  295. case 'Q':
  296. return $this->formatInfo->FullTimePattern;
  297. break;
  298. case 'f':
  299. return $this->formatInfo->formatDateTime($this->formatInfo->LongDatePattern, $this->formatInfo->ShortTimePattern);
  300. break;
  301. case 'F':
  302. return $this->formatInfo->formatDateTime($this->formatInfo->LongDatePattern, $this->formatInfo->LongTimePattern);
  303. break;
  304. case 'g':
  305. return $this->formatInfo->formatDateTime($this->formatInfo->ShortDatePattern, $this->formatInfo->ShortTimePattern);
  306. break;
  307. case 'G':
  308. return $this->formatInfo->formatDateTime($this->formatInfo->ShortDatePattern, $this->formatInfo->LongTimePattern);
  309. break;
  310. case 'i':
  311. return 'yyyy-MM-dd';
  312. break;
  313. case 'I':
  314. return 'yyyy-MM-dd HH:mm:ss';
  315. break;
  316. case 'M':
  317. case 'm':
  318. return 'MMMM dd';
  319. break;
  320. case 'R':
  321. case 'r':
  322. return 'EEE, dd MMM yyyy HH:mm:ss';
  323. break;
  324. case 's':
  325. return 'yyyy-MM-ddTHH:mm:ss';
  326. break;
  327. case 'u':
  328. return 'yyyy-MM-dd HH:mm:ss z';
  329. break;
  330. case 'U':
  331. return 'EEEE dd MMMM yyyy HH:mm:ss';
  332. break;
  333. case 'Y':
  334. case 'y':
  335. return 'yyyy MMMM';
  336. break;
  337. default :
  338. return $pattern;
  339. }
  340. }
  341. /**
  342. * Returns an easy to parse input pattern
  343. * yy is replaced by yyyy and h by H
  344. *
  345. * @param string $pattern pattern.
  346. * @return string input pattern
  347. */
  348. public function getInputPattern($pattern)
  349. {
  350. $pattern = $this->getPattern($pattern);
  351. $pattern = strtr($pattern, array('yyyy' => 'Y', 'h'=>'H', 'z'=>'', 'a'=>''));
  352. $pattern = strtr($pattern, array('yy'=>'yyyy', 'Y'=>'yyyy'));
  353. return trim($pattern);
  354. }
  355. /**
  356. * Tokenizes the pattern. The tokens are delimited by group of
  357. * similar characters, e.g. 'aabb' will form 2 tokens of 'aa' and 'bb'.
  358. * Any substrings, starting and ending with a single quote (')
  359. * will be treated as a single token.
  360. *
  361. * @param string $pattern pattern.
  362. * @return array string tokens in an array.
  363. */
  364. protected function getTokens($pattern)
  365. {
  366. $char = null;
  367. $tokens = array();
  368. $token = null;
  369. $text = false;
  370. for ($i = 0, $max = strlen($pattern); $i < $max; $i++)
  371. {
  372. if ($char == null || $pattern{$i} == $char || $text)
  373. {
  374. $token .= $pattern{$i};
  375. }
  376. else
  377. {
  378. $tokens[] = str_replace("''", "'", $token);
  379. $token = $pattern{$i};
  380. }
  381. if ($pattern{$i} == "'" && $text == false)
  382. {
  383. $text = true;
  384. }
  385. else if ($text && $pattern{$i} == "'" && $char == "'")
  386. {
  387. $text = true;
  388. }
  389. else if ($text && $char != "'" && $pattern{$i} == "'")
  390. {
  391. $text = false;
  392. }
  393. $char = $pattern{$i};
  394. }
  395. $tokens[] = $token;
  396. return $tokens;
  397. }
  398. // makes a unix date from our incomplete $date array
  399. protected function getUnixDate($date)
  400. {
  401. return getdate(mktime($date['hours'], $date['minutes'], $date['seconds'], $date['mon'], $date['mday'], $date['year']));
  402. }
  403. /**
  404. * Gets the year.
  405. * "yy" will return the last two digits of year.
  406. * "y", "yyy" and "yyyy" will return the full integer year.
  407. *
  408. * @param array $date getdate format.
  409. * @param string $pattern a pattern.
  410. * @return string year
  411. */
  412. protected function getYear($date, $pattern = 'yyyy')
  413. {
  414. $year = $date['year'];
  415. switch ($pattern)
  416. {
  417. case 'yy':
  418. return substr($year, 2);
  419. case 'y':
  420. case 'yyy':
  421. case 'yyyy':
  422. return $year;
  423. default:
  424. throw new sfException('The pattern for year is either "y", "yy", "yyy" or "yyyy".');
  425. }
  426. }
  427. /**
  428. * Gets the month.
  429. * "M" will return integer 1 through 12
  430. * "MM" will return integer 1 through 12 padded with 0 to two characters width
  431. * "MMM" will return the abrreviated month name, e.g. "Jan"
  432. * "MMMM" will return the month name, e.g. "January"
  433. * "MMMMM" will return the narrow month name, e.g. "J"
  434. *
  435. * @param array $date getdate format.
  436. * @param string $pattern a pattern.
  437. * @return string month name
  438. */
  439. protected function getMon($date, $pattern = 'M')
  440. {
  441. $month = $date['mon'];
  442. switch ($pattern)
  443. {
  444. case 'M':
  445. return $month;
  446. case 'MM':
  447. return str_pad($month, 2, '0', STR_PAD_LEFT);
  448. case 'MMM':
  449. return $this->formatInfo->AbbreviatedMonthNames[$month - 1];
  450. case 'MMMM':
  451. return $this->formatInfo->MonthNames[$month - 1];
  452. case 'MMMMM':
  453. return $this->formatInfo->NarrowMonthNames[$month - 1];
  454. default:
  455. throw new sfException('The pattern for month is "M", "MM", "MMM", "MMMM", "MMMMM".');
  456. }
  457. }
  458. /**
  459. * Gets the day of the week.
  460. * "E" will return integer 0 (for Sunday) through 6 (for Saturday).
  461. * "EE" will return the narrow day of the week, e.g. "M"
  462. * "EEE" will return the abrreviated day of the week, e.g. "Mon"
  463. * "EEEE" will return the day of the week, e.g. "Monday"
  464. *
  465. * @param array $date getdate format.
  466. * @param string $pattern a pattern.
  467. * @return string day of the week.
  468. */
  469. protected function getWday($date, $pattern = 'EEEE')
  470. {
  471. // if the $date comes from our home-made get date
  472. if (!isset($date['wday']))
  473. {
  474. $date = $this->getUnixDate($date);
  475. }
  476. $day = $date['wday'];
  477. switch ($pattern)
  478. {
  479. case 'E':
  480. return $day;
  481. break;
  482. case 'EE':
  483. return $this->formatInfo->NarrowDayNames[$day];
  484. case 'EEE':
  485. return $this->formatInfo->AbbreviatedDayNames[$day];
  486. break;
  487. case 'EEEE':
  488. return $this->formatInfo->DayNames[$day];
  489. break;
  490. default:
  491. throw new sfException('The pattern for day of the week is "E", "EE", "EEE", or "EEEE".');
  492. }
  493. }
  494. /**
  495. * Gets the day of the month.
  496. * "d" for non-padding, "dd" will always return 2 characters.
  497. *
  498. * @param array $date getdate format.
  499. * @param string $pattern a pattern.
  500. * @return string day of the month
  501. */
  502. protected function getMday($date, $pattern = 'd')
  503. {
  504. $day = $date['mday'];
  505. switch ($pattern)
  506. {
  507. case 'd':
  508. return $day;
  509. case 'dd':
  510. return str_pad($day, 2, '0', STR_PAD_LEFT);
  511. case 'dddd':
  512. return $this->getWday($date);
  513. default:
  514. throw new sfException('The pattern for day of the month is "d", "dd" or "dddd".');
  515. }
  516. }
  517. /**
  518. * Gets the era. i.e. in gregorian, year > 0 is AD, else BC.
  519. *
  520. * @todo How to support multiple Eras?, e.g. Japanese.
  521. * @param array $date getdate format.
  522. * @param string $pattern a pattern.
  523. * @return string era
  524. */
  525. protected function getEra($date, $pattern = 'G')
  526. {
  527. if ($pattern != 'G')
  528. {
  529. throw new sfException('The pattern for era is "G".');
  530. }
  531. return $this->formatInfo->getEra($date['year'] > 0 ? 1 : 0);
  532. }
  533. /**
  534. * Gets the hours in 24 hour format, i.e. [0-23].
  535. * "H" for non-padding, "HH" will always return 2 characters.
  536. *
  537. * @param array $date getdate format.
  538. * @param string $pattern a pattern.
  539. * @return string hours in 24 hour format.
  540. */
  541. protected function getHours($date, $pattern = 'H')
  542. {
  543. $hour = $date['hours'];
  544. switch ($pattern)
  545. {
  546. case 'H':
  547. return $hour;
  548. case 'HH':
  549. return str_pad($hour, 2, '0', STR_PAD_LEFT);
  550. default:
  551. throw new sfException('The pattern for 24 hour format is "H" or "HH".');
  552. }
  553. }
  554. /**
  555. * Get the AM/PM designator, 12 noon is PM, 12 midnight is AM.
  556. *
  557. * @param array $date getdate format.
  558. * @param string $pattern a pattern.
  559. * @return string AM or PM designator
  560. */
  561. protected function getAMPM($date, $pattern = 'a')
  562. {
  563. if ($pattern != 'a')
  564. {
  565. throw new sfException('The pattern for AM/PM marker is "a".');
  566. }
  567. return $this->formatInfo->AMPMMarkers[intval($date['hours'] / 12)];
  568. }
  569. /**
  570. * Gets the hours in 12 hour format.
  571. * "h" for non-padding, "hh" will always return 2 characters.
  572. *
  573. * @param array $date getdate format.
  574. * @param string $pattern a pattern.
  575. * @return string hours in 12 hour format.
  576. */
  577. protected function getHour12($date, $pattern = 'h')
  578. {
  579. $hour = $date['hours'];
  580. $hour = ($hour == 12 | $hour == 0) ? 12 : $hour % 12;
  581. switch ($pattern)
  582. {
  583. case 'h':
  584. return $hour;
  585. case 'hh':
  586. return str_pad($hour, 2, '0', STR_PAD_LEFT);
  587. default:
  588. throw new sfException('The pattern for 24 hour format is "H" or "HH".');
  589. }
  590. }
  591. /**
  592. * Gets the minutes.
  593. * "m" for non-padding, "mm" will always return 2 characters.
  594. *
  595. * @param array $date getdate format.
  596. * @param string $pattern a pattern.
  597. * @return string minutes.
  598. */
  599. protected function getMinutes($date, $pattern = 'm')
  600. {
  601. $minutes = $date['minutes'];
  602. switch ($pattern)
  603. {
  604. case 'm':
  605. return $minutes;
  606. case 'mm':
  607. return str_pad($minutes, 2, '0', STR_PAD_LEFT);
  608. default:
  609. throw new sfException('The pattern for minutes is "m" or "mm".');
  610. }
  611. }
  612. /**
  613. * Gets the seconds.
  614. * "s" for non-padding, "ss" will always return 2 characters.
  615. *
  616. * @param array $date getdate format.
  617. * @param string $pattern a pattern.
  618. * @return string seconds
  619. */
  620. protected function getSeconds($date, $pattern = 's')
  621. {
  622. $seconds = $date['seconds'];
  623. switch ($pattern)
  624. {
  625. case 's':
  626. return $seconds;
  627. case 'ss':
  628. return str_pad($seconds, 2, '0', STR_PAD_LEFT);
  629. default:
  630. throw new sfException('The pattern for seconds is "s" or "ss".');
  631. }
  632. }
  633. /**
  634. * Gets the timezone from the server machine.
  635. *
  636. * @todo How to get the timezone for a different region?
  637. * @param array $date getdate format.
  638. * @param string $pattern a pattern.
  639. * @return string time zone
  640. */
  641. protected function getTimeZone($date, $pattern = 'z')
  642. {
  643. //mapping to PHP pattern symbols
  644. switch ($pattern)
  645. {
  646. case 'z':
  647. $pattern = 'T';
  648. break;
  649. case 'Z':
  650. $pattern = 'O';
  651. default:
  652. throw new sfException('The pattern for time zone is "z" or "Z".');
  653. }
  654. return @date($pattern, @mktime($date['hours'], $date['minutes'], $date['seconds'], $date['mon'], $date['mday'], $date['year']));
  655. }
  656. /**
  657. * Gets the day in the year, e.g. [1-366]
  658. *
  659. * @param array $date getdate format.
  660. * @param string $pattern a pattern.
  661. * @return int hours in AM/PM format.
  662. */
  663. protected function getYday($date, $pattern = 'D')
  664. {
  665. if ($pattern != 'D')
  666. {
  667. throw new sfException('The pattern for day in year is "D".');
  668. }
  669. return $date['yday'];
  670. }
  671. /**
  672. * Gets day in the month.
  673. *
  674. * @param array $date getdate format.
  675. * @param string $pattern a pattern.
  676. * @return int day in month
  677. */
  678. protected function getDayInMonth($date, $pattern = 'FF')
  679. {
  680. switch ($pattern)
  681. {
  682. case 'F':
  683. return @date('j', @mktime(0, 0, 0, $date['mon'], $date['mday'], $date['year']));
  684. break;
  685. case 'FF':
  686. return @date('d', @mktime(0, 0, 0, $date['mon'], $date['mday'], $date['year']));
  687. break;
  688. default:
  689. throw new sfException('The pattern for day in month is "F" or "FF".');
  690. }
  691. }
  692. /**
  693. * Gets the week in the year.
  694. *
  695. * @param array $date getdate format.
  696. * @param string $pattern a pattern.
  697. * @return int week in year
  698. */
  699. protected function getWeekInYear($date, $pattern = 'w')
  700. {
  701. if ($pattern != 'w')
  702. {
  703. throw new sfException('The pattern for week in year is "w".');
  704. }
  705. return @date('W', @mktime(0, 0, 0, $date['mon'], $date['mday'], $date['year']));
  706. }
  707. /**
  708. * Gets week in the month.
  709. *
  710. * @param array $date getdate format.
  711. * @param string $pattern a pattern
  712. * @return int week in month
  713. */
  714. protected function getWeekInMonth($date, $pattern = 'W')
  715. {
  716. if ($pattern != 'W')
  717. {
  718. throw new sfException('The pattern for week in month is "W".');
  719. }
  720. return @date('W', @mktime(0, 0, 0, $date['mon'], $date['mday'], $date['year'])) - date('W', mktime(0, 0, 0, $date['mon'], 1, $date['year']));
  721. }
  722. /**
  723. * Gets the hours [1-24].
  724. *
  725. * @param array $date getdate format.
  726. * @param string $pattern a pattern.
  727. * @return int hours [1-24]
  728. */
  729. protected function getHourInDay($date, $pattern = 'k')
  730. {
  731. if ($pattern != 'k')
  732. {
  733. throw new sfException('The pattern for hour in day is "k".');
  734. }
  735. return $date['hours'] + 1;
  736. }
  737. /**
  738. * Gets the hours in AM/PM format, e.g [1-12]
  739. *
  740. * @param array $date getdate format.
  741. * @param string $pattern a pattern.
  742. * @return int hours in AM/PM format.
  743. */
  744. protected function getHourInAMPM($date, $pattern = 'K')
  745. {
  746. if ($pattern != 'K')
  747. {
  748. throw new sfException('The pattern for hour in AM/PM is "K".');
  749. }
  750. return ($date['hours'] + 1) % 12;
  751. }
  752. }