Join Kevin Skoglund for an in-depth discussion in this video PHP DateTime, part of Easy PHP Projects: Time Zone Conversion.
- [Voiceover] In this movie, we'll learn how to use the PHP DateTime class. PHP DateTime class is object-oriented programming. That's what working with a class means, and in general, object-oriented DateTime is better then working with the procedural. We saw the procedural functions in the last movie, and those are fine. They're very useful especially for dealing with time stamps because they just return a simple value to you. That's handy, however, when we work with objects, objects can contain knowledge about the date and time.
They can contain more than just a simple time stamp and we can ask the object questions. That's very handy and very useful. We'll see how that works. As I mentioned earlier, PHP has many other procedural functions for working with date and time. But those procedural functions have object-oriented equivalents where the first argument is an object. So for example, we have DateTime, getTimezone. Well there's also a procedural version called date_timezone_get and guess what it's first argument is? A date time object.
So if you have to have an object then you might as well just use the object oriented version. There's no reason or benefit to having the procedural around at all. If you don't have a lot of experience with object-oriented programming, don't worry, the DateTime class is a great place to get started. Let me show you a quick example of how it works. Here's an example of object-oriented programming using the DateTime class that also introduces you to the first five functions you need to know. In the first line, we're instantiating a new object. We're basically just saying, "Hey, PHP.
"Build me a DateTime object." We do that by saying new in front of the class name. So new space and the class name and then any arguments that that object needs. For the DateTime class, we're going to be passing in a string telling it how it should build it. This is very similar to what we were doing when we were using string to time in order to build a string. It accepts a wide variety of things. It can accept tomorrow, or first Monday July 2018, all of those things are possible.
After we've instantiated that object, we want to assign it to a variable. I've used $dt here for my variable name for date time. Then I can ask it questions. So that's what I'm doing in the next line. I'm echoing back the response and I'm saying hey dt object, call your format function. I do that by putting the -> sign it makes like an arrow. So I'm saying object, call your format function and I pass in the format string. Very similar to what we saw in the last movie when we were outputing using date.
These are the date format codes, not string f time, but instead the date formats. So I've got those there, and then after that I say, "Okay object," still got the object around, "Tell me your time stamp." I do that using get time stamp. Then I can tell it to set the time stamp and I can pass in a time stamp and now my DateTime object won't be March 1st 2018 anymore, now it's going to be my current time. Just like that, my object has shifted and changed, and I could ask it to output the date again.
It would give me a different answer now because I've changed it using set time stamp, and the last of all, I call modify, and modify also takes those relatives strings, like we can normally pass in to when we were first creating DateTime for example, it takes those relative times and allows us to modify times that way. So notice, I'm creating a new object, I've got format, getTimestamp, setTimestamp, and modify. Those are the first five functions that you need to know and this is how object-oriented programming works.
Let me demonstrate these as PHP code in a browser. So you see I've got a new file in my site's directory called datetime_demo.php. You'll also find this in your exercise files. Let's bring that up. Take a look at what's in it. So it's exactly the same thing we were just working with here, I've just added a little more formatting to it. I've put a string in front of these that say date and time stamp. I've put some br tags at the end, but it's basically the same thing. When I'm changing the time stamp to a new time stamp, I'm using string to time in order to create the time stamp and then adding eight hours to it.
This is the number of seconds, minutes, and hours. So adding that together to get my new timestamp and then I'm using setTimestamp to make my DateTime object switch to that time, and then from there I'm modifying it by adding one year. Each time I'm using format to output it one more time so we can see what those look like. All right, so let's save that and let's go over to Firefox and check it out. Local host, for me that's kevinskoglund and datetime_demo.php.
So there we go. The first one comes up after it creates the object it outputs the formatted date, gives me Thursday March 1st 2018 and the time, which is midnight. Notice that I didn't tell it that it was Thursday, right? That's the kind of knowledge that's built into this object, this object knows that this date is a Thursday, and then I have it output the time stamp for me. So I can see what that time stamp is. Then I've got another one here, where I've changed it to June 15th 2017 at 8:00 AM. Notice, June 15th 2017, 8:00 AM is because I added that time to it to get the time stamp and then I said go one year from that and we get the same thing June 15th on 2018.
So all of that I'm doing using this object-oriented program. It's a really easy way to get your feet wet with object-oriented programming if you haven't done it before. So as I said, those are the first five DateTime functions you need to know about, but there's three more that are important. All of them are related to timezones. getTimezone, setTimezone, and get0ffset. But as you can see from the second line there where we call getTimezone, what it returns to us is a timezone object, or technically an instance of the class DateTimezone, and we haven't talked about DateTimezone objects yet, so it's difficult to talk about these and how these work until we know about that other class as well.
So let's look at those in the next movie.
- Understanding the history and purpose of time zones
- Setting a default time zone in PHP
- Using the PHP DateTime and DateTimeZone classes
- Working with daylight saving time transitions
- Allowing users to select their time zone
- Displaying times in the user's preferred time zone
- Calculating the current times of major cities
- Creating a complete time zone calculator