PHP has a code class called Date Time.
This will help you in reading, writing, comparing dates, or
making date time rated calculations.
There are many date and
time rated functions in PHP besides the date time class, but
this provides a nice object oriented interface to most of the common uses.
So let’s start off by opening index for PHP.
On line three, we’re going to create a new date time object and
store it in the date variable.
In this case,
we'll be creating a object by passing a string into the class constructor.
This string will contain a date, which can be in one of a bunch of different formats.
We'll look at some of these formats shortly, but for
now, this example has an input format of year, comma, August 23.
On line seven we're outputting a paragraph of the text.
The output day is, then using the format method on
our date time object output the date in our own specific format.
Our example here use M slash D slash capital Y is an output format.
Meaning when we run this script we should expect to see 08/23/2014.
Let's take a look in preview mode, by clicking on the eye icon.
Cool, that works.
Here we have the output date is 08/23/2014, just like we expected.
The benefit here is that we can change the input date string, and
have the same output format.
One alternative commonly used in put format, is the SQL date string.
Let's go back to the work space.
Back to index.php and let's enter a new date and a new format.
So I'm gonna keep the year 2014.
I'm gonna change things around a little bit and enter 09-12.
Which is the 12th of September.
I save that out.
Then refresh.
Great, we have the exact same date format, but it has a different date.
If we go out back to the workspace, we can get a little bit more adventurous with
initializing date time objects using string offsets.
We can change the constructor to take a string starting with a plus or a minus.
Then we add number of units and
define the unit with the words second, hours, days, weeks, months, or even years.
So if we enter plus two weeks and have a little look, see what happens there.
Then we will have an output which is two weeks from now.
Brilliant.
You can jump forwards using more human-friendly phrases, too.
We can change this string to contain phrases like next week or tomorrow.
So let's have a go at that.
Next week.
Perfect.
Tomorrow.
There we go.
As I mentioned earlier the string argument passed to the date time constructor on
line three can accept dates in many different input formats.
To learn more about the date time formats we can use the PHP manual.
There is a link in the notes for
this video called PHP date formats so let's head over there.
[SOUND] You scroll about half way down.
You can see under localized notations there's a whole bunch of
different formats supported.
Some American ones.
Some more generic.
The format here.
This is a literal string.
So that means it's actually a slash, and not some special meaning.
And then you can see some actual examples of how they look.
So, when you use them they'll come out without the quotation marks around them.
And there's some other fairly common formats here.
This one, like the four digit year, month, and
day with slashes could be one that you might want to use.
Sometimes you will need to work with dates that are stored in unsupported formats,
for example if a string contains two date segments with numeric values below 13 they
could both be days or months.
Luckily the date time class has a method called create from format we can
provide our custom input format to this method along with our date so
that PHP can read the string correctly.
If we go back to our workspace, paste this code in.
On line three here we have a string containing an unsupported date format.
We store it in the raw variable and pass it to the create from format method.
If all goes well,
when we run this once again, we should see our date output in the same format.
This time the date should be 11/10/1968.
Save that and see if it works.
Perfect!
Let's move on.