mirror of
https://github.com/codeguy/php-the-right-way.git
synced 2025-08-21 12:52:01 +02:00
Fix line wrapping consistency - round 1
Comply with the 'wrap at 120 chars' style rule stated in the contributing.md document
This commit is contained in:
@@ -5,13 +5,14 @@ anchor: date_and_time
|
||||
|
||||
## Date and Time {#date_and_time_title}
|
||||
|
||||
PHP has a class named DateTime to help you when reading, writing, comparing or calculating with date and time. There are
|
||||
many date and time related functions in PHP besides DateTime, but it provides nice object-oriented interface to most
|
||||
common uses. It can handle time zones, but that is outside this short introduction.
|
||||
PHP has a class named DateTime to help you when reading, writing, comparing or calculating with date and time. There
|
||||
are many date and time related functions in PHP besides DateTime, but it provides nice object-oriented interface to
|
||||
most common uses. It can handle time zones, but that is outside this short introduction.
|
||||
|
||||
To start working with DateTime, convert raw date and time string to an object with `createFromFormat()` factory method
|
||||
or do `new DateTime` to get the current date and time. Use `format()` method to convert DateTime back to a string for
|
||||
output.
|
||||
|
||||
{% highlight php %}
|
||||
<?php
|
||||
$raw = '22. 11. 1968';
|
||||
@@ -22,8 +23,9 @@ echo 'Start date: ' . $start->format('Y-m-d') . "\n";
|
||||
|
||||
Calculating with DateTime is possible with the DateInterval class. DateTime has methods like `add()` and `sub()` that
|
||||
take a DateInterval as an argument. Do not write code that expect same number of seconds in every day, both daylight
|
||||
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.
|
||||
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.
|
||||
|
||||
{% highlight php %}
|
||||
<?php
|
||||
// create a copy of $start and add one month and 6 days
|
||||
@@ -36,6 +38,7 @@ echo 'Difference: ' . $diff->format('%m month, %d days (total: %a days)') . "\n"
|
||||
{% endhighlight %}
|
||||
|
||||
On DateTime objects you can use standard comparison:
|
||||
|
||||
{% highlight php %}
|
||||
<?php
|
||||
if ($start < $end) {
|
||||
@@ -45,6 +48,7 @@ if ($start < $end) {
|
||||
|
||||
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.
|
||||
|
||||
{% highlight php %}
|
||||
<?php
|
||||
// output all thursdays between $start and $end
|
||||
|
Reference in New Issue
Block a user