home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Calendar / Util / Textual.php next >
Encoding:
PHP Script  |  2008-07-02  |  8.3 KB  |  239 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. //
  4. // +----------------------------------------------------------------------+
  5. // | PHP Version 4                                                        |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2002 The PHP Group                                |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.02 of the PHP license,      |
  10. // | that is bundled with this package in the file LICENSE, and is        |
  11. // | available at through the world-wide-web at                           |
  12. // | http://www.php.net/license/3_0.txt.                                  |
  13. // | If you did not receive a copy of the PHP license and are unable to   |
  14. // | obtain it through the world-wide-web, please send a note to          |
  15. // | license@php.net so we can mail you a copy immediately.               |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Harry Fuecks <hfuecks@phppatterns.com>                      |
  18. // |          Lorenzo Alberton <l dot alberton at quipo dot it>           |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: Textual.php,v 1.2 2004/08/16 13:13:09 hfuecks Exp $
  22. //
  23. /**
  24.  * @package Calendar
  25.  * @version $Id: Textual.php,v 1.2 2004/08/16 13:13:09 hfuecks Exp $
  26.  */
  27.  
  28. /**
  29.  * Allows Calendar include path to be redefined
  30.  * @ignore
  31.  */
  32. if (!defined('CALENDAR_ROOT')) {
  33.     define('CALENDAR_ROOT', 'Calendar'.DIRECTORY_SEPARATOR);
  34. }
  35.  
  36. /**
  37.  * Load Calendar decorator base class
  38.  */
  39. require_once CALENDAR_ROOT.'Decorator.php';
  40.  
  41. /**
  42.  * Static utlities to help with fetching textual representations of months and
  43.  * days of the week.
  44.  * @package Calendar
  45.  * @access public
  46.  */
  47. class Calendar_Util_Textual
  48. {
  49.  
  50.     /**
  51.      * Returns an array of 12 month names (first index = 1)
  52.      * @param string (optional) format of returned months (one,two,short or long)
  53.      * @return array
  54.      * @access public
  55.      * @static
  56.      */
  57.     function monthNames($format='long')
  58.     {
  59.         $formats = array('one'=>'%b', 'two'=>'%b', 'short'=>'%b', 'long'=>'%B');
  60.         if (!array_key_exists($format,$formats)) {
  61.             $format = 'long';
  62.         }
  63.         $months = array();
  64.         for ($i=1; $i<=12; $i++) {
  65.             $stamp = mktime(0, 0, 0, $i, 1, 2003);
  66.             $month = strftime($formats[$format], $stamp);
  67.             switch($format) {
  68.                 case 'one':
  69.                     $month = substr($month, 0, 1);
  70.                 break;
  71.                 case 'two':
  72.                     $month = substr($month, 0, 2);
  73.                 break;
  74.             }
  75.             $months[$i] = $month;
  76.         }
  77.         return $months;
  78.     }
  79.  
  80.     /**
  81.      * Returns an array of 7 week day names (first index = 0)
  82.      * @param string (optional) format of returned days (one,two,short or long)
  83.      * @return array
  84.      * @access public
  85.      * @static
  86.      */
  87.     function weekdayNames($format='long')
  88.     {
  89.         $formats = array('one'=>'%a', 'two'=>'%a', 'short'=>'%a', 'long'=>'%A');
  90.         if (!array_key_exists($format,$formats)) {
  91.             $format = 'long';
  92.         }
  93.         $days = array();
  94.         for ($i=0; $i<=6; $i++) {
  95.             $stamp = mktime(0, 0, 0, 11, $i+2, 2003);
  96.             $day = strftime($formats[$format], $stamp);
  97.             switch($format) {
  98.                 case 'one':
  99.                     $day = substr($day, 0, 1);
  100.                 break;
  101.                 case 'two':
  102.                     $day = substr($day, 0, 2);
  103.                 break;
  104.             }
  105.             $days[$i] = $day;
  106.         }
  107.         return $days;
  108.     }
  109.  
  110.     /**
  111.      * Returns textual representation of the previous month of the decorated calendar object
  112.      * @param object subclass of Calendar e.g. Calendar_Month
  113.      * @param string (optional) format of returned months (one,two,short or long)
  114.      * @return string
  115.      * @access public
  116.      * @static
  117.      */
  118.     function prevMonthName($Calendar, $format='long')
  119.     {
  120.         $months = Calendar_Util_Textual::monthNames($format);
  121.         return $months[$Calendar->prevMonth()];
  122.     }
  123.  
  124.     /**
  125.      * Returns textual representation of the month of the decorated calendar object
  126.      * @param object subclass of Calendar e.g. Calendar_Month
  127.      * @param string (optional) format of returned months (one,two,short or long)
  128.      * @return string
  129.      * @access public
  130.      * @static
  131.      */
  132.     function thisMonthName($Calendar, $format='long')
  133.     {
  134.         $months = Calendar_Util_Textual::monthNames($format);
  135.         return $months[$Calendar->thisMonth()];
  136.     }
  137.  
  138.     /**
  139.      * Returns textual representation of the next month of the decorated calendar object
  140.      * @param object subclass of Calendar e.g. Calendar_Month
  141.      * @param string (optional) format of returned months (one,two,short or long)
  142.      * @return string
  143.      * @access public
  144.      * @static
  145.      */
  146.     function nextMonthName($Calendar, $format='long')
  147.     {
  148.         $months = Calendar_Util_Textual::monthNames($format);
  149.         return $months[$Calendar->nextMonth()];
  150.     }
  151.  
  152.     /**
  153.      * Returns textual representation of the previous day of week of the decorated calendar object
  154.      * <b>Note:</b> Requires PEAR::Date
  155.      * @param object subclass of Calendar e.g. Calendar_Month
  156.      * @param string (optional) format of returned months (one,two,short or long)
  157.      * @return string
  158.      * @access public
  159.      * @static
  160.      */
  161.     function prevDayName($Calendar, $format='long')
  162.     {
  163.         $days = Calendar_Util_Textual::weekdayNames($format);
  164.         $stamp = $Calendar->prevDay('timestamp');
  165.         $cE = $Calendar->getEngine();
  166.         require_once 'Date/Calc.php';
  167.         $day = Date_Calc::dayOfWeek($cE->stampToDay($stamp),
  168.             $cE->stampToMonth($stamp), $cE->stampToYear($stamp));
  169.         return $days[$day];
  170.     }
  171.  
  172.     /**
  173.      * Returns textual representation of the day of week of the decorated calendar object
  174.      * <b>Note:</b> Requires PEAR::Date
  175.      * @param object subclass of Calendar e.g. Calendar_Month
  176.      * @param string (optional) format of returned months (one,two,short or long)
  177.      * @return string
  178.      * @access public
  179.      * @static
  180.      */
  181.     function thisDayName($Calendar, $format='long')
  182.     {
  183.         $days = Calendar_Util_Textual::weekdayNames($format);
  184.         require_once 'Date/Calc.php';
  185.         $day = Date_Calc::dayOfWeek($Calendar->thisDay(), $Calendar->thisMonth(), $Calendar->thisYear());
  186.         return $days[$day];
  187.     }
  188.  
  189.     /**
  190.      * Returns textual representation of the next day of week of the decorated calendar object
  191.      * @param object subclass of Calendar e.g. Calendar_Month
  192.      * @param string (optional) format of returned months (one,two,short or long)
  193.      * @return string
  194.      * @access public
  195.      * @static
  196.      */
  197.     function nextDayName($Calendar, $format='long')
  198.     {
  199.         $days = Calendar_Util_Textual::weekdayNames($format);
  200.         $stamp = $Calendar->nextDay('timestamp');
  201.         $cE = $Calendar->getEngine();
  202.         require_once 'Date/Calc.php';
  203.         $day = Date_Calc::dayOfWeek($cE->stampToDay($stamp),
  204.             $cE->stampToMonth($stamp), $cE->stampToYear($stamp));
  205.         return $days[$day];
  206.     }
  207.  
  208.     /**
  209.      * Returns the days of the week using the order defined in the decorated
  210.      * calendar object. Only useful for Calendar_Month_Weekdays, Calendar_Month_Weeks
  211.      * and Calendar_Week. Otherwise the returned array will begin on Sunday
  212.      * @param object subclass of Calendar e.g. Calendar_Month
  213.      * @param string (optional) format of returned months (one,two,short or long)
  214.      * @return array ordered array of week day names
  215.      * @access public
  216.      * @static
  217.      */
  218.     function orderedWeekdays($Calendar, $format='long')
  219.     {
  220.         $days = Calendar_Util_Textual::weekdayNames($format);
  221.         
  222.         // Not so good - need methods to access this information perhaps...
  223.         if (isset($Calendar->tableHelper)) {
  224.             $ordereddays = $Calendar->tableHelper->daysOfWeek;
  225.         } else {
  226.             $ordereddays = array(0, 1, 2, 3, 4, 5, 6);
  227.         }
  228.         
  229.         $ordereddays = array_flip($ordereddays);
  230.         $i = 0;
  231.         $returndays = array();
  232.         foreach ($ordereddays as $key => $value) {
  233.             $returndays[$i] = $days[$key];
  234.             $i++;
  235.         }
  236.         return $returndays;
  237.     }
  238. }
  239. ?>