title | isChild | anchor |
---|---|---|
Date and Time |
true |
date_and_time |
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. DateTime can handle time zones, but that is outside the scope of 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 %}
format('Y-m-d') . PHP_EOL; {% endhighlight %} 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 expects the same number of seconds in every day. Both daylight saving and time zone 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 %} add(new DateInterval('P1M6D')); $diff = $end->diff($start); echo 'Difference: ' . $diff->format('%m month, %d days (total: %a days)') . PHP_EOL; // Difference: 1 month, 6 days (total: 37 days) {% endhighlight %} You can use standard comparisons on DateTime objects: {% highlight php %} format('Y-m-d') . ' '; } {% endhighlight %} A popular PHP API extension is [Carbon](https://carbon.nesbot.com/). It inherits everything in the DateTime class, so involves minimal code alterations, but extra features include Localization support, further ways to add, subtract and format a DateTime object, plus a means to test your code by simulating a date and time of your choosing. * [Read about DateTime][datetime] * [Read about date formatting][dateformat] (accepted date format string options) [datetime]: https://www.php.net/book.datetime [dateformat]: https://www.php.net/function.date