Introduction to Zend Framework

 Learning Zend Framework

appendix

 Zend Framework Reference


  •  Zend_Gdata
  •  Zend_Http
  •  Zend_InfoCard
  •  Zend_Json
  •  Zend_Layout
  •  Zend_Ldap
  •  Zend_Loader
  •  Zend_Locale
  •  Zend_Log
  •  Zend_Mail
  •  Zend_Markup
  •  Zend_Measure
  •  Zend_Memory
  •  Zend_Mime
  •  Zend_Navigation
  •  Zend_Oauth
  •  Zend_OpenId
  •  Zend_Paginator
  •  Zend_Pdf
  •  Zend_ProgressBar
  •  Zend_Queue
  •  Zend_Reflection
  •  Zend_Registry
  •  Zend_Rest

  •  Zend_Search_Lucene
  •  Zend_Serializer
  •  Zend_Server
  •  Zend_Service
  •  Zend_Session
  •  Zend_Soap
  •  Zend_Tag
  •  Zend_Test
  •  Zend_Text
  •  Zend_TimeSync
  •  Zend_Tool
  •  Zend_Tool_Framework
  •  Zend_Tool_Project
  •  Zend_Translate
  •  Zend_Uri
  •  Zend_Validate
  •  Zend_Version
  •  Zend_View
  •  Zend_Wildfire
  •  Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Update 2011-11-16 - Revision 24438 - Version ZF 1.11.x

    43.4. Working with Dates and Times

    Zend_Locale_Format provides several methods for working with dates and times to help convert and normalize between different formats for different locales. Use Zend_Date for manipulating dates, and working with date strings that already conform to one of the many internationally recognized standard formats, or one of the localized date formats supported by Zend_Date. Using an existing, pre-defined format offers advantages, including the use of well-tested code, and the assurance of some degree of portability and interoperability (depending on the standard used). The examples below do not follow these recommendations, since using non-standard date formats would needlessly increase the difficulty of understanding these examples.

    43.4.1. Normalizing Dates and Times

    The getDate() method parses strings containing dates in localized formats. The results are returned in a structured array, with well-defined keys for each part of the date. In addition, the array will contain a key 'date_format' showing the format string used to parse the input date string. Since a localized date string may not contain all parts of a date/time, the key-value pairs are optional. for example, if only the year, month, and day is given, then all time values are suppressed from the returned array, and vice-versa if only hour, minute, and second were given as input. If no date or time can be found within the given input, an exception will be thrown.

    If setOption(array('fix_date' => true)) is set the getDate() method adds a key 'fixed' with a whole number value indicating if the input date string required "fixing" by rearranging the day, month, or year in the input to fit the format used.

    Table 43.5. Key values for getDate() with option 'fix_date'

    value meaning
    0 nothing to fix
    1 fixed false month
    2 swapped day and year
    3 swapped month and year
    4 swapped month and day

    For those needing to specify explicitly the format of the date string, the following format token specifiers are supported. If an invalid format specifier is used, such as the PHP 'i' specifier when in ISO format mode, then an error will be thrown by the methods in Zend_Locale_Format that support user-defined formats.

    These specifiers (below) are a small subset of the full "ISO" set supported by Zend_Date's toString(). If you need to use PHP date() compatible format specifiers, then first call setOptions(array('format_type' => 'php')). And if you want to convert only one special format string from PHP date() compatible format to "ISO" format use convertPhpToIsoFormat(). Currently, the only practical difference relates to the specifier for minutes ('m' using the ISO default, and 'i' using the PHP date format).

    Table 43.6. Return values

    getDate() format character Array key Returned value Minimum Maximum
    d day integer 1 31
    M month integer 1 12
    y year integer no limit PHP integer's maximum
    h hour integer 0 PHP integer's maximum
    m minute integer 0 PHP integer's maximum
    s second integer 0 PHP integer's maximum

    Example 43.43. Normalizing a date

    $dateString Zend_Locale_Format::getDate('13.04.2006',
                                              array(
    'date_format' =>
                                                        
    'dd.MM.yyyy')
                                             );

    // creates a Zend_Date object for this date
    $dateObject Zend_Date('13.04.2006',
                             array(
    'date_format' => 'dd.MM.yyyy'));

    print_r($dateString); // outputs:

    Array
    (
        [
    format] => dd.MM.yyyy
        
    [day] => 13
        
    [month] => 4
        
    [year] => 2006
    )

    // alternatively, some types of problems with input data can be
    // automatically corrected
    $date Zend_Locale_Format::getDate('04.13.2006',
                                        array(
    'date_format' => 'dd.MM.yyyy',
                                              
    'fix_date' => true)
                                       );

    print_r($date); // outputs:

    Array
    (
        [
    format] => dd.MM.yyyy
        
    [day] => 13
        
    [month] => 4
        
    [year] => 2006
        
    [fixed] => 4
    )

    Since getDate() is "locale-aware", specifying the $locale is sufficient for date strings adhering to that locale's format. The option 'fix_date' uses simple tests to determine if the day or month is not valid, and then applies heuristics to try and correct any detected problems. Note the use of 'Zend_Locale_Format::STANDARD' as the value for 'date_format' to prevent the use of a class-wide default date format set using setOptions(). This forces getDate to use the default date format for $locale.

    Example 43.44. Normalizing a date by locale

    $locale = new Zend_Locale('de_AT');
    $date Zend_Locale_Format::getDate('13.04.2006',
                                        array(
    'date_format' =>
                                                  
    Zend_Locale_Format::STANDARD,
                                              
    'locale' => $locale)
                                       );

    print_r ($date);

    A complete date and time is returned when the input contains both a date and time in the expected format.

    Example 43.45. Normalizing a date with time

    $locale = new Zend_Locale('de_AT');
    $date Zend_Locale_Format::getDate('13.04.2005 22:14:55',
                                        array(
    'date_format' =>
                                                    
    Zend_Locale_Format::STANDARD,
                                              
    'locale' => $locale)
                                        );

    print_r ($date);

    If a specific format is desired, specify the $format argument, without giving a $locale. Only single-letter codes (H, m, s, y, M, d), and MMMM and EEEE are supported in the $format.

    Example 43.46. Normalizing a userdefined date

    $date Zend_Locale_Format::getDate('13200504T551422',
                                        array(
    'date_format' =>
                                                  
    'ddyyyyMM ssmmHH')
                                       );

    print_r ($date);

    The format can include the following signs :

    Table 43.7. Format definition

    Format Letter Description
    d or dd 1 or 2 digit day
    M or MM 1 or 2 digit month
    y or yy 1 or 2 digit year
    yyyy 4 digit year
    h 1 or 2 digit hour
    m 1 or 2 digit minute
    s 1 or 2 digit second

    Examples for proper formats are

    Table 43.8. Example formats

    Formats Input Output
    dd.MM.yy 1.4.6 ['day'] => 1, ['month'] => 4, ['year'] => 6
    dd.MM.yy 01.04.2006 ['day'] => 1, ['month'] => 4, ['year'] => 2006
    yyyyMMdd 1.4.6 ['day'] => 6, ['month'] => 4, ['year'] => 1

    [Note] Database date format

    To parse a database date value (f.e. MySql or MsSql), use Zend_Date's ISO_8601 format instead of getDate().

    The option 'fix_date' uses simple tests to determine if the day or month is not valid, and then applies heuristics to try and correct any detected problems. getDate() automatically detects and corrects some kinds of problems with input, such as misplacing the year:

    Example 43.47. Automatic correction of input dates

    $date Zend_Locale_Format::getDate('41.10.20',
                                        array(
    'date_format' => 'ddMMyy',
                                              
    'fix_date' => true)
                                       );

    // instead of 41 for the day, the 41 will be returned as year value
    print_r ($date);

    43.4.2. Testing Dates

    Use checkDateFormat($inputString, array('date_format' => $format, $locale)) to check if a given string contains all expected date parts. The checkDateFormat() method uses getDate(), but without the option 'fixdate' to avoid returning TRUE when the input fails to conform to the date format. If errors are detected in the input, such as swapped values for months and days, the option 'fixdate' method will apply heuristics to "correct" dates before determining their validity.

    Example 43.48. Date testing

    $locale = new Zend_Locale('de_AT');
    // using the default date format for 'de_AT', is this a valid date?
    if (Zend_Locale_Format::checkDateFormat('13.Apr.2006',
                                            array(
    'date_format' =>
                                                      
    Zend_Locale_Format::STANDARD,
                                                  
    $locale)
                                           ) {
        print 
    "date";
    } else {
        print 
    "not a date";
    }

    43.4.3. Normalizing a Time

    Normally, a time will be returned with a date, if the input contains both. If the proper format is not known, but the locale relevant to the user input is known, then getTime() should be used, because it uses the default time format for the selected locale.

    Example 43.49. Normalize an unknown time

    $locale = new Zend_Locale('de_AT');
    if (
    Zend_Locale_Format::getTime('13:44:42',
                                    array(
    'date_format' =>
                                              
    Zend_Locale_Format::STANDARD,
                                          
    'locale' => $locale)) {
        print 
    "time";
    } else {
        print 
    "not a time";
    }

    43.4.4. Testing Times

    Use checkDateFormat() to check if a given string contains a proper time. The usage is exact the same as with checking Dates, only date_format should contain the parts which you expect to have.

    Example 43.50. Testing a time

    $locale = new Zend_Locale('de_AT');
    if (
    Zend_Locale_Format::checkDateFormat('13:44:42',
                                            array(
    'date_format' => 'HH:mm:ss',
                                                  
    'locale' => $locale)) {
        print 
    "time";
    } else {
        print 
    "not a time";
    }

    digg delicious meneame google twitter technorati facebook

    Comments

    Loading...