cvs: peardoc /en/package datetime.xml /en/package/datetime date.xml /en/package/datetime/date examples.xml

From: Date: Fri, 29 Jun 2007 05:37:02 +0000
Subject: cvs: peardoc /en/package datetime.xml /en/package/datetime date.xml /en/package/datetime/date examples.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-9846@lists.php.net to get a copy of this message
cweiske Fri Jun 29 05:37:02 2007 UTC Added files: /peardoc/en/package/datetime date.xml /peardoc/en/package/datetime/date examples.xml Modified files: /peardoc/en/package datetime.xml Log: After seven years without docs, some examples using Date http://cvs.php.net/viewvc.cgi/peardoc/en/package/datetime.xml?r1=1.7&r2=1.8&diff_format=u Index: peardoc/en/package/datetime.xml diff -u peardoc/en/package/datetime.xml:1.7 peardoc/en/package/datetime.xml:1.8 --- peardoc/en/package/datetime.xml:1.7 Fri Feb 17 16:04:31 2006 +++ peardoc/en/package/datetime.xml Fri Jun 29 05:37:02 2007 @@ -1,4 +1,5 @@ -<!-- $Revision: 1.7 $ --> +<?xml version="1.0" encoding="utf-8"?> +<!-- $Revision: 1.8 $ --> <chapter id="package.datetime"> <title>Date and Time</title> @@ -10,8 +11,9 @@ &blankpagehack; &package.datetime.calendar; + &package.datetime.date; &package.datetime.date-holidays; - + </chapter> <!-- Keep this comment at the end of the file Local variables: http://cvs.php.net/viewvc.cgi/peardoc/en/package/datetime/date.xml?view=markup&rev=1.1 Index: peardoc/en/package/datetime/date.xml +++ peardoc/en/package/datetime/date.xml <!-- $Revision: 1.1 $ --> <sect1 id="package.datetime.date"> <title>Date</title> <para> PEAR <classname>Date</classname> is a generic class to represent and work with dates. It does not use timestamps which means it can be used for dates before 1970 and after 2038. The class provides methods to convert a date in different formats, calculate differences, weekdays and more. </para> &package.datetime.date.examples; </sect1> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/datetime/date/examples.xml?view=markup&rev=1.1 Index: peardoc/en/package/datetime/date/examples.xml +++ peardoc/en/package/datetime/date/examples.xml <?xml version="1.0" encoding="utf-8"?> <!-- $Revision: 1.1 $ --> <sect2 id="package.datetime.date.examples"> <title>Examples</title> <para> Following the saying "learning by doing", we start with some examples. </para> <para> The first step to work with the class is to instantiate a <classname>Date</classname> object. </para> <example> <title>Creating a Date object</title> <programlisting role="php"><![CDATA[ <?php require_once 'Date.php'; //without parameter == now $now = new Date(); //pass a string in ISO format $longAgo = new Date('1813-02-23T05:34:23'); //create a Date object from a PHP unix timestamp $timestamp = time(); $nearlyNow = new Date($timestamp); //make a copy of an existing Date object $copyDate = new Date($longAgo); ?> ]]></programlisting> </example> <para> After finishing your work with the date object, you probably want to have it back. This can be done in different ways. </para> <example> <title>Getting the date in different formats</title> <programlisting role="php"><![CDATA[ <?php require_once 'Date.php'; $now = new Date(); //UNIX timestamp: 1183054688 echo $now->getTime(); //ISO formatted date: 2007-06-28 20:18:08 echo $now->getDate(); ?> ]]></programlisting> </example> <para> Date has a lot more methods to output your date, but that is subject of a later chapter. <!-- FIXME: link later chapter --> </para> <para> Now that we know how to create a <classname>Date</classname> instance, we'll do some easy tasks. </para> <sect3 id="package.datetime.date.examples.timediff"> <title>Calculating a time difference</title> <para> You often have the task to know which time span lies between two dates. With <classname>Date</classname>, this is easy to accomplish. </para> <example> <title>Calculating a time span</title> <programlisting role="php"><![CDATA[ <?php require_once 'Date.php'; $someDate = new Date('1813-02-23T05:34:23'); $otherDate = new Date('1714-12-21T18:23:42'); $span = new Date_Span(); $span->setFromDateDiff($someDate, $otherDate); //time span in days: 8463,46575231 echo $span->toDays(); //time span in full years: 23 echo (int)($span->toDays() / 365); ?> ]]></programlisting> </example> <note> <para> <classname>Date_Span</classname> works, unlike <classname>Date</classname>, internally with integers, which means that you have a precision of 32 bit or 68 years. </para> </note> </sect3> <sect3 id="package.datetime.date.examples.converttimezone"> <title>Converting timezones</title> <para> <classname>Date</classname> can help you working with time zones. An array with all supported timezones can be retrieved by using <command>$list = Date_TimeZone::getAvailableIDs();</command>. </para> <example> <title>Converting timezones</title> <programlisting role="php"><![CDATA[ <?php require_once 'Date.php'; //assume it's 2007-06-28 18:42:12 $now = new Date(); $timezone = new Date_TimeZone('Australia/Adelaide'); //convert the date object to the timezone $now->convertTZ($timezone); //will give you: 2007-06-29 02:12:12 echo $now->getDate(); //now with timezone: 2007-06-29 02:12:12+09:30 echo $now->format('%Y-%m-%d %H:%M:%S%O'); //switch back $defaultZone = Date_TimeZone::getDefault(); $now->convertTZ($defaultZone); //we have our normal zone now: 2007-06-28 18:42:12+02:00 echo $now->format('%Y-%m-%d %H:%M:%S%O'); ?> ]]></programlisting> </example> </sect3> <sect3 id="package.datetime.date.examples.sorting"> <title>Sorting an array of dates</title> <para> Once you have an array of <classname>Date</classname> objects, you might want to sort it. The class provides a static method <methodname>compare</methodname> that helps with this. </para> <example> <title>Sorting dates</title> <programlisting role="php"><![CDATA[ <?php require_once 'Date.php'; $dates = array( new Date('1813-02-23T05:34:23'), new Date(), new Date('1714-12-21T18:23:42'), ); usort($dates, array('Date', 'compare')); /* * prints the dates correctly sorted: * 1714-12-21 18:23:42 * 1813-02-23 05:34:23 * 2007-06-28 20:59:39 */ foreach ($dates as $date) { echo $date->getDate() . "\n"; } ?> ]]></programlisting> </example> </sect3> </sect2> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->

« previous php.pear.doc (#9846) next »