View Video Playlists Full Easy PHP Projects: Time Zone Conversion https://www.youtube.com/playlist?list=PLSJUvA2v3Fs4doBlJM2ueYuP8Sz5OVhf- - [Voiceover] In this movie, we'll learn how to use the PHP DateTimeZone class. DateTimeZone is a PHP class, and you use it in an object-oriented style, like we did with DateTime. You call new followed by the class name to create a new DateTimeZone instance. The argument that you need to provide is one of the time zone identifiers we discussed earlier. You can in the first line that I'm instantiating a new object using the time zone identifier for America/New_York and assigning that to the variable TZ. Then I can call the get name function on that DateTimeZone object and it will output the name of the time zone. Next, I call the function getLocation, which returns an array of location information. We'll see what's inside that array when we demo this code in a moment. And last, I wanted to remind you that the DateTimeZone class contains the list of time zone identifiers. We saw this previously when I mentioned that it was an alternative to the procedural time zone identifiers list function that we put in our TZ identifiers .php script. We won't go back over that again here but I just wanted to mention that it is part of the DateTimeZone class. A DateTimeZone object is mostly useful when we apply it to a DateTime object. Here in this code, I create a new DateTimeZone object. Next, I create a new DateTime object. Then I can set the time zone using DateTime's setTimezone function. Notice that the 'z' in time zone is lowercase here when it's uppercase in the class name before. It's one of the quirks of PHP. You can also pass in a DateTimeZone object as the second argument when you're first creating a new DateTime object to have it set right at the start. So, I've got an example of that there in the fourth line. Now, the DateTime functions for getTimezone and getOffset also make more sense. Notice the getTimezone does not return the time zone name, it returns a DateTimeZone object. So, if we wanna show the name, we have to get that object and then ask that object to return its name. Let's try all of this with PHP and a browser. In my Sites Directory you can see that I've got a new file called datetimezone_demo.php. That's also included in your exercise files. And this is what it looks like. It's very similar to what we just saw in the slides. I'm creating a new DateTimeZone object using America/New_York. You can use a different one if you'd like. I'm assigning that to TZ. Then I'm asking it to get it's name. So, we'll see what the name is that it returns back to us. Next, I'm asking it for it's location information. Now, getLocation returns that but it's an array of information. So, now I'm going through that location array and I'm outputting all of the keys and values. I'm got an asterisk, key, colon, and then value for each of those lines. Then down here on the second part of the page, I'm gonna show some of the different ways we can work with DateTime and TimeZones. So, here I've got a DateTime for March 1, 2018. I'm just instantiating that and assigning to DT. I'm gonna first tell it to get the time zone, and show me the name that it has for that time zone. I'm doing that from that TimeZone object I'm calling $tz. So, that's gonna show me the default time zone, the one that's set by default. Cause I'm not passing anything in here, and I haven't set anything. So, this is the default that's in my system. That is why it's so important that we have that default set in our php.ini file or at the top of our script. I'm also gonna tell it to show me what the offset is for that. Notice that I'm calling getOffset on the DateTime object not on the TimeZone object. Then, I'm gonna change the DateTime to new time zone. So, I've created one here I'm calling $losangeles_tz. And I'm gonna instantiate a new time zone. And I'm gonna set DT to have that new time zone. So, now whatever my default was I've now changed it to be Los Angeles. Then I'm gonna have basically the same code here these lines are pretty much what we had up here. But it's just gonna say that it's the Los Angeles one. It's gonna show me the name and the offset. And then I'm gonna do the same thing using Tokyo. So, I've got a Tokyo time zone, a new DateTimeZone I'm instantiating. This time I'm creating a DateTime from scratch.