mirror of
https://github.com/codeguy/php-the-right-way.git
synced 2025-08-08 06:56:33 +02:00
Merge pull request #132 from grakic/gh-pages_datetime2
Date and time: Fix syntax highlight
This commit is contained in:
@@ -24,6 +24,7 @@ take a DateInterval as an argument. Do not write code that expect same number of
|
|||||||
saving and timezone alterations will break that assumption. Use date intervals instead. To calculate date difference use
|
saving and timezone alterations will break that assumption. Use date intervals instead. To calculate date difference use
|
||||||
the `diff()` method. It will return new DateInterval, which is super easy to display.
|
the `diff()` method. It will return new DateInterval, which is super easy to display.
|
||||||
{% highlight php %}
|
{% highlight php %}
|
||||||
|
<?php
|
||||||
// create a copy of $start and add one month and 6 days
|
// create a copy of $start and add one month and 6 days
|
||||||
$end = clone $start;
|
$end = clone $start;
|
||||||
$end->add(new \DateInterval('P1M6D'));
|
$end->add(new \DateInterval('P1M6D'));
|
||||||
@@ -35,6 +36,7 @@ echo "Difference: " . $diff->format('%m month, %d days (total: %a days)') . "\n"
|
|||||||
|
|
||||||
On DateTime objects you can use standard comparison:
|
On DateTime objects you can use standard comparison:
|
||||||
{% highlight php %}
|
{% highlight php %}
|
||||||
|
<?php
|
||||||
if($start < $end) {
|
if($start < $end) {
|
||||||
echo "Start is before end!\n";
|
echo "Start is before end!\n";
|
||||||
}
|
}
|
||||||
@@ -43,6 +45,7 @@ if($start < $end) {
|
|||||||
One last example to demonstrate the DatePeriod class. It is used to iterate over recurring events. It can take two
|
One last example to demonstrate the DatePeriod class. It is used to iterate over recurring events. It can take two
|
||||||
DateTime objects, start and end, and the interval for which it will return all events in between.
|
DateTime objects, start and end, and the interval for which it will return all events in between.
|
||||||
{% highlight php %}
|
{% highlight php %}
|
||||||
|
<?php
|
||||||
// output all thursdays between $start and $end
|
// output all thursdays between $start and $end
|
||||||
$periodInterval = \DateInterval::createFromDateString('first thursday');
|
$periodInterval = \DateInterval::createFromDateString('first thursday');
|
||||||
$periodIterator = new \DatePeriod($start, $periodInterval, $end, \DatePeriod::EXCLUDE_START_DATE);
|
$periodIterator = new \DatePeriod($start, $periodInterval, $end, \DatePeriod::EXCLUDE_START_DATE);
|
||||||
|
Reference in New Issue
Block a user