Reworked examples to new oop/procedural combination format. Changed several titles. Simplified some initial examples and moved some parts to new examples. Per discussion on php-doc.

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@298705 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Daniel Convissor 2010-04-28 15:58:26 +00:00
parent 076bad363e
commit 0e42d1d3a6
13 changed files with 321 additions and 70 deletions

View file

@ -56,14 +56,39 @@
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>date_add</function> example</title>
<title><function>DateTime::add</function> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d H:i:s') . "\n";
echo $date->format('Y-m-d') . "\n";
?>
]]>
</programlisting>
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('10 days'));
echo date_format($date, 'Y-m-d');
?>
]]>
</programlisting>
&examples.outputs;
<screen>
<![CDATA[
2000-01-11
]]>
</screen>
</example>
<example>
<title>Further <function>DateTime::add</function> examples</title>
<programlisting role="php">
<![CDATA[
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
@ -77,7 +102,6 @@ echo $date->format('Y-m-d H:i:s') . "\n";
&example.outputs;
<screen>
<![CDATA[
2000-01-11 00:00:00
2000-01-01 10:00:30
2007-06-05 04:03:02
]]>

View file

@ -82,6 +82,33 @@
&reftitle.examples;
<example>
<title><function>DateTime::__construct</function> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d');
?>
]]>
</programlisting>
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = date_create('2000-01-01');
echo date_format($date, 'Y-m-d');
?>
]]>
</programlisting>
&examples.outputs;
<screen>
<![CDATA[
2000-01-01
]]>
</screen>
</example>
<example>
<title>Intricacies of <function>DateTime::__construct</function></title>
<programlisting role="php">
<![CDATA[
<?php

View file

@ -93,7 +93,34 @@
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><methodname>DateTime::createFromFormat</methodname> example</title>
<title><function>DateTime::createFromFormat</function> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
]]>
</programlisting>
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = date_create_from_format('j-M-Y', '15-Feb-2009');
echo date_format($date, 'Y-m-d');
?>
]]>
</programlisting>
&examples.outputs;
<screen>
<![CDATA[
2009-02-15
]]>
</screen>
</example>
<example>
<title>Intricacies of <function>DateTime::createFromFormat</function></title>
<programlisting role="php">
<![CDATA[
<?php

View file

@ -59,7 +59,8 @@
<refsect1 role="examples">
&reftitle.examples;
<example>
<title>Object oriented style example of <function>DateTime::diff</function></title>
<title><function>DateTime::diff</function> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
@ -70,21 +71,19 @@ echo $interval->format('%R%d days');
?>
]]>
</programlisting>
</example>
<example>
<title>Procedural style example of <function>date_diff</function></title>
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%d days');
?>
]]>
</programlisting>
</example>
&example.outputs;
&examples.outputs;
<screen>
<![CDATA[
+2 days

View file

@ -47,24 +47,23 @@
<refsect1 role="examples">
&reftitle.examples;
<example>
<title>Displaying the date and time using the procedural form</title>
<programlisting role="php">
<![CDATA[
<?php
$date = date_create('2000-01-01');
echo date_format($date, 'Y-m-d H:i:s');
?>
]]>
</programlisting>
</example>
<example>
<title>Displaying the date and time using the object oriented form</title>
<title><function>DateTime::format</function> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');
?>
]]>
</programlisting>
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = date_create('2000-01-01');
echo date_format($date, 'Y-m-d H:i:s');
?>
]]>
</programlisting>
&example.outputs;

View file

@ -40,15 +40,32 @@
&reftitle.examples;
<example>
<title><function>DateTime::getLastErrors</function> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
try {
$date = new DateTime('asdfasdf');
} catch (Exception $e) {
// For demonstration purposes only...
print_r(DateTime::getLastErrors());
// The real object oriented way to do this is
// echo $e->getMessage();
}
?>
]]>
</programlisting>
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = date_create('asdfasdf');
print_r(DateTime::getLastErrors());
print_r(date_get_last_errors());
?>
]]>
</programlisting>
&example.outputs;
&examples.outputs;
<screen>
<![CDATA[
Array

View file

@ -39,18 +39,41 @@
<refsect1 role="examples">
&reftitle.examples;
<example>
<title>Comparing offsets between Summer and Winter</title>
<title><function>DateTime::getOffset</function> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
$winter = new DateTime('2010-12-21', new DateTimeZone('America/New_York'));
$summer = new DateTime('2008-06-21', new DateTimeZone('America/New_York'));
echo $winter->getOffset() . "\n"; // "-18000" (-5 hours)
echo $summer->getOffset() . "\n"; // "-14400" (-4 hours)
echo $winter->getOffset() . "\n";
echo $summer->getOffset() . "\n";
?>
]]>
</programlisting>
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
$winter = new date_create('2010-12-21', timezone_open('America/New_York'));
$summer = new date_create('2008-06-21', timezone_open('America/New_York'));
echo $winter->getOffset() . "\n";
echo $summer->getOffset() . "\n";
?>
]]>
</programlisting>
&examples.outputs;
<screen>
<![CDATA[
-18000
-14400
]]>
</screen>
<para>
Note: -18000 = -5 hours, -14400 = -4 hours.
</para>
</example>
</refsect1>

View file

@ -39,6 +39,7 @@
&reftitle.examples;
<example>
<title><function>DateTime::getTimezone</function> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
@ -48,7 +49,17 @@ echo $tz->getName();
?>
]]>
</programlisting>
&example.outputs;
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = date_create(null, timezone_open('Europe/London'));
$tz = date_timezone_get($date);
echo timezone_name_get($tz);
?>
]]>
</programlisting>
&examples.outputs;
<screen>
<![CDATA[
Europe/London

View file

@ -87,42 +87,57 @@
<refsect1 role="examples">
&reftitle.examples;
<example>
<title>Object oriented example usage</title>
<title><function>DateTime::setDate</function> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = new DateTime();
$date->setDate(2001, 2, 3);
echo $date->format('Y-m-d');
?>
]]>
</programlisting>
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = date_create();
date_date_set($date, 2001, 2, 3);
echo date_format($date, 'Y-m-d');
?>
]]>
</programlisting>
&examples.outputs;
<screen>
<![CDATA[
2001-02-03
]]>
</screen>
</example>
<example>
<title>Values exceeding ranges are added to their parent values</title>
<programlisting role="php">
<![CDATA[
<?php
$date = new DateTime();
$date->setDate(2001, 2, 3);
$date->setDate(2001, 2, 28);
echo $date->format('Y-m-d') . "\n";
$date->setDate(2001, 2, 29);
echo $date->format('Y-m-d') . "\n";
// Values exceeding ranges are added to their parent values.
$date->setDate(2001, 14, 3);
echo $date->format('Y-m-d') . "\n";
?>
]]>
</programlisting>
</example>
<example>
<title>Procedural example usage</title>
<programlisting role="php">
<![CDATA[
<?php
$date = date_create();
date_date_set($date, 2001, 2, 3);
echo date_format($date, 'Y-m-d') . "\n";
// Values exceeding ranges are added to their parent values.
date_date_set($date, 2001, 14, 3);
echo date_format($date, 'Y-m-d') . "\n";
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
2001-02-03
2001-02-28
2001-03-01
2002-02-03
]]>
</screen>

View file

@ -88,7 +88,8 @@
<refsect1 role="examples">
&reftitle.examples;
<example>
<title>Finding the date from a week number and day offset</title>
<title><function>DateTime::setISODate</function> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
@ -97,25 +98,57 @@ $date = new DateTime();
$date->setISODate(2008, 2);
echo $date->format('Y-m-d') . "\n";
$date->setISODate(2008, 2, 1);
$date->setISODate(2008, 2, 7);
echo $date->format('Y-m-d') . "\n";
?>
]]>
</programlisting>
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = date_create();
date_isodate_set($date, 2008, 2);
echo date_format($date, 'Y-m-d') . "\n";
date_isodate_set($date, 2008, 2, 7);
echo date_format($date, 'Y-m-d') . "\n";
?>
]]>
</programlisting>
&examples.outputs;
<screen>
<![CDATA[
2008-01-07
2008-01-13
]]>
</screen>
</example>
<example>
<title>Values exceeding ranges are added to their parent values</title>
<programlisting role="php">
<![CDATA[
<?php
$date = new DateTime();
$date->setISODate(2008, 2, 7);
echo $date->format('Y-m-d') . "\n";
// Values exceeding ranges are added to their parent values.
$date->setISODate(2008, 2, 8);
echo $date->format('Y-m-d') . "\n";
$date->setISODate(2008, 53, 7);
echo $date->format('Y-m-d') . "\n";
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
2008-01-07
2008-01-07
2008-01-13
2008-01-14
2009-01-04
]]>
</screen>
</example>

View file

@ -87,32 +87,71 @@
<refsect1 role="examples">
&reftitle.examples;
<example>
<title>Changing the time of a DateTime object</title>
<title><function>DateTime::setTime</function> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s') . "\n";
$date = new DateTime('2001-01-01');
$date->setTime(14, 55);
echo $date->format('Y-m-d H:i:s') . "\n";
$date->setTime(14, 55, 24);
echo $date->format('Y-m-d H:i:s') . "\n";
?>
]]>
</programlisting>
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = date_create('2001-01-01');
date_time_set($date, 14, 55);
echo date_format($date, 'Y-m-d H:i:s') . "\n";
date_time_set($date, 14, 55, 24);
echo date_format($date, 'Y-m-d H:i:s') . "\n";
?>
]]>
</programlisting>
&examples.outputs.similar;
<screen>
<![CDATA[
2000-01-01 14:55:00
2000-01-01 14:55:24
]]>
</screen>
</example>
<example>
<title>Values exceeding ranges are added to their parent values</title>
<programlisting role="php">
<![CDATA[
<?php
$date = new DateTime('2001-01-01');
$date->setTime(14, 55, 24);
echo $date->format('Y-m-d H:i:s') . "\n";
$date->setTime(14, 55, 65);
echo $date->format('Y-m-d H:i:s') . "\n";
// Values exceeding ranges are added to their parent values.
$date->setTime(14, 65, 24);
echo $date->format('Y-m-d H:i:s') . "\n";
$date->setTime(25, 55, 24);
echo $date->format('Y-m-d H:i:s') . "\n";
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
2000-01-01 00:00:00
2000-01-01 14:55:00
2000-01-01 14:55:24
2000-01-01 15:05:24
2001-01-01 14:55:24
2001-01-01 14:56:05
2001-01-01 15:05:24
2001-01-02 01:55:24
]]>
</screen>
</example>

View file

@ -71,7 +71,8 @@
<refsect1 role="examples">
&reftitle.examples;
<example>
<title>Setting and getting DateTimeZone objects</title>
<title><function>DateTime::setTimeZone</function> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
@ -83,7 +84,19 @@ echo $date->format('Y-m-d H:i:sP') . "\n";
?>
]]>
</programlisting>
&example.outputs;
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = date_create('2000-01-01', timezone_open('Pacific/Nauru'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
date_timezone_set($date, timezone_open('Pacific/Chatham'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
?>
]]>
</programlisting>
&examples.outputs;
<screen>
<![CDATA[
2000-01-01 00:00:00+12:00

View file

@ -56,14 +56,39 @@
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>date_sub</function> example</title>
<title><function>DateTime::sub</function> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = new DateTime('2000-01-20');
$date->sub(new DateInterval('P10D'));
echo $date->format('Y-m-d H:i:s') . "\n";
echo $date->format('Y-m-d') . "\n";
?>
]]>
</programlisting>
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = date_create('2000-01-20');
date_sub($date, date_interval_create_from_date_string('10 days'));
echo date_format($date, 'Y-m-d');
?>
]]>
</programlisting>
&examples.outputs;
<screen>
<![CDATA[
2000-01-10
]]>
</screen>
</example>
<example>
<title>Further <function>DateTime::sub</function> examples</title>
<programlisting role="php">
<![CDATA[
<?php
$date = new DateTime('2000-01-20');
$date->sub(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
@ -77,7 +102,6 @@ echo $date->format('Y-m-d H:i:s') . "\n";
&example.outputs;
<screen>
<![CDATA[
2000-01-10 00:00:00
2000-01-19 13:59:30
1992-08-15 19:56:58
]]>