Введение в Zend Framework

 Learning Zend Framework


 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
  • Ttranslation 27.4% Update 2010-11-28 - Revision 23238 - Version ZF 1.11.x

    26.3. Basic Methods

    The following sections show basic usage of Zend_Date primarily by example. For this manual, "dates" always imply a calendar date with a time, even when not explicitly mentioned, and vice-versa. The part not specified defaults to an internal representation of "zero". Thus, adding a date having no calendar date and a time value of 12 hours to another date consisting only of a calendar date would result in a date having that calendar date and a time of "noon".

    Setting only a specific date, with no time part, implies a time set to 00:00:00. Conversely, setting only a specific time implies a date internally set to 01.01.1970 plus the number of seconds equal to the elapsed hours, minutes, and seconds identified by the time. Normally, people measure things from a starting point, such as the year 0 A.D. However, many software systems use the first second of the year 1970 as the starting point, and denote times as a timestamp offset counting the number of seconds elapsed from this starting point.

    26.3.1. Current Date

    Without any arguments, constructing an instance returns an object in the default locale with the current, local date using PHP's time() function to obtain the UNIX timestamp for the object. Make sure your PHP environment has the correct default timezone.

    Пример 26.2. Creating the Current Date

    $date = new Zend_Date();

    // Output of the current timestamp
    print $date;

    26.3.2. Zend_Date by Example

    Reviewing basic methods of Zend_Date is a good place to start for those unfamiliar with date objects in other languages or frameworks. A small example will be provided for each method below. Output a Date

    The date in a Zend_Date object may be obtained as a localized integer or string using the get() method. There are many available options, which will be explained in later sections.

    Пример 26.3. get() - Output a Date

    $date = new Zend_Date();

    // Output of the desired date
    print $date->get(); Setting a Date

    The set() method alters the date stored in the object, and returns the final date value as a timestamp (not an object). Again, there are many options which will be explored in later sections.

    Пример 26.4. set() - Set a Date

    $date = new Zend_Date();

    // Setting of a new time
    $date->get(Zend_Date::W3C); Adding and Subtracting Dates

    Adding two dates with add() usually involves adding a real date in time with an artificial timestramp representing a date part, such as 12 hours, as shown in the example below. Both add() and sub() use the same set of options as set(), which will be explained later.

    Пример 26.5. add() - Adding Dates

    $date = new Zend_Date();

    // changes $date by adding 12 hours

    "Date via get() = "$date->get(Zend_Date::W3C), "\n";

    // use magic __toString() method to call Zend_Date's toString()
    echo "Date via toString() = "$date"\n"; Comparison of Dates

    All basic Zend_Date methods can operate on entire dates contained in the objects, or can operate on date parts, such as comparing the minutes value in a date to an absolute value. For example, the current minutes in the current time may be compared with a specific number of minutes using compare(), as in the example below.

    Пример 26.6. compare() - Compare Dates

    $date = new Zend_Date();

    // Comparation of both times
    if ($date->compare(10Zend_Date::MINUTE) == -1) {
    "This hour is less than 10 minutes old";
    } else {
    "This hour is at least 10 minutes old";

    For simple equality comparisons, use equals(), which returns a boolean.

    Пример 26.7. equals() - Identify a Date or Date Part

    $date = new Zend_Date();

    // Comparation of the two dates
    if ($date->equals(10Zend_Date::HOUR)) {
    "It's 10 o'clock. Time to get to work.";
    } else {
    "It is not 10 o'clock. You can keep sleeping.";

    digg delicious meneame google twitter technorati facebook