How to get the beginning of a day¶
Just like with any other development task, first I try to identify what I need. As the title goes, “how to get the beginning of a day”.
Thus, I need the beginning of any given day. As a result, there is a class which is called exactly like that:
TheBeginningOfADay
. In turn, what is it? It’s a specific datetime actually. Hence no wonder that this class extends ISO8601DateTime
abstract class.
So, obtaining the beginning of any given day from a datetime goes like the following:
(new TheBeginningOfADay(
new FromISO8601('2014-11-21 08:12:54-11:30')
))
->value();
If you need the beginning of a current datetime, it goes like that:
(new TheBeginningOfADay(
new Now()
))
->value();
If you want to get just a date from a datetime, things change. What you need is a date, not a datetime. Thus, the class
you should use definitely implements a Date
interface (or a corresponding abstract class). The class we need in our case
is a FromISO8601DateTime
. It’s read as A date obtained from an ISO8601 datetime
, and an abstract class implies that
there is a single property that an object can tell you about: it is its textual representation, or value
, which is just more concise.
Here goes the code:
(new FromISO8601DateTime(
new FromISO8601('2017-07-03T00:00:00+00:00')
))
->value(); // results into '2017-07-03' string
If you want to format it some peculiar way, you have two options: either built-in php ISO8601 formatting facility, or, if you want to localize your datetime, you’d want to opt into the IntlDateFormatter class.