Date and Time Manipulation Functions

Hey guys welcome back in this video we will start looking at date and time manipulation using PHP. Now as we’ve been doing so far, the first thing we do is create a page for date and time manipulation. So I’ll just reuse this one and say date time manip dot php, and rename the link to say date and time manipulation. Next up is to create our page. So we call it the date time manip dot php. And then we do our html5 boilerplate, change our title, put in our h1 tag, which we always start with and just copy and paste Then we start with our PHP tags. Now our first challenge today is what if we wanted to get a timestamp? How do I know what time it is right now? So really and truly the time, right? No is relative to the time on the machine or the server that you are using meaning of the time on my machine right now, which is my server is wrong, because it is, well, it’s actually after 3pm, right? No, but this server time is it 59 All right. So when I do get time, I am going to get to whatever time is on the machine, so the machine time is wrong, then your time in your application is also going to be misrepresented. If that is not the desired results. Not withstanding that, we can just go ahead and echo and echo inside of a p tag. So just open and close the P tags, same time. And I am going to go. And I’m going to see if this works get it. Alright, so that doesn’t work. And that’s because this is a function call and not a variable. Remember, we’re talking about interpolation. And also I’m using single quotes and double quotes. So let me just change that code. And then we can go ahead and adjust. So what I’m going to do is firstly, declare a variable and I’m going to call it date. No. All right, and then I’m going to meet this variable have the value that is get it all right and open and close quotation marks. So whenever this page is going to be run. This code once it’s executed is going to get the exact date and time on the machine when the page loaded. So to get it echoed without any hassle, I’m just going to Using the interpolation and put in date, no. All right, so it’s just going to inject it in there, I notice it’s blue in the orange text. Alright. So now I’m going to go back to my Index page, and then just click on the link to get to the new page. And we see that we have a slight error here. So this error is seeing that we’re trying to put an array to string and that is not a load, all right? No, let me explain this arrow as it is a very valid arrow, get date is actually going to return an array. So this day no is really an array and we looked at arrays earlier in our exploration. Alright, so an array is a contiguous collection of data. Know what we didn’t look at was the ability to actually reference the subscripts using named was named subscripts as opposed to numerical subscripts. So in our original record, and I can just go back there We looked at having an array of just numbers and then looking at each number based on the position it was in inside of the array. When we look at date and time manipulation, we looked at the get date, the date No, actually storing each value offer dates, which would be the month or the year, or the day of the week, in a named subscript, so we’re going to actually go through that so I can’t print all this array just equal the array, it won’t work after equal the value from the array that I wish to see. Alright, so I’m just going to write a bit more code here. And then I’m going to call today’s date. And if you look at it, I’m using the double quotation mark because I need a single quote inside. So that’s going back to the other thing about knowing which one you start with and which one you need to end with. So I’m using double quotes because I need a single quotes inside today’s date. All right, and then I am I’m going to read Klein. And then in the next slide, I am going to show you our next few lines. I’ll just duplicate them, and I will show you each subscript. So the first named subscript that I’m going to use is m, d. So let’s see how that works. Now you notice the red line underneath it, because really, and truly this, there are limitations to the interpolation, I’m trying to be a bit too fancy. So I’m just showing you that there are some limitations to what you can do when you’re trying to do the interpolation. When it comes to the named variables, because we have the presence of the quotation marks and so on, you might get some errors. So what I’m going to do is just remove these tags and take it out of the quotation marks. I’m just printing the variable, but then I’m going to concatenate a break tag to make sure that the next line gets printed by itself. All right, so then when I refresh this page, and look, I’m still getting these arrows because I still have that duplicated code trying to print the array to string but then this line is producing the 21. Because why I am doing this video on the 21st of September. So MD is where you get the beat the day, the actual day of the month MD, I guess that’s one day. So that’s the day of the month. Alright. And then similarly, I can and I have to do the same with these because of the reference and named subscript. So I can just modify the string here and just concatenate a br tag here. Alright, so the name that subscript for the month itself would be so open the square brackets just like I did in the previous video. I opened this square bracket. But this time instead of using a number of these a string because a named, it’s a named subscripts. So I’m using the string mn. Alright, so that’s something new about arrays, an array can be referenced by position, or it can just be referenced by the name, it depends on how you created an array or whole the original array is. So we had no control over how this one does it, but this is what it expects. So when I do that, I’m going to get the number nine because nine is the number associated with September and we get this loop down here I look up and see nine slash 21 2019. Alright, and then in the same way, I’m just going to duplicate this since we have the winning formula No, and just remove that line. And then I want the year someone to write the word year so named subscript for the year is the word year, and I refresh that we get to into 19. All our errors are gone. I know we have three Correct, those are three values rather, that can give us the date. So I can actually print the date in one line. So I’ll just echo and see two days. And then notice, notice what’s happening here with my echo. It’s giving me an arrow because I’m using the quotation mark here, quotation mark here and there. So because I want the single quote there, I can just wrap it all in a double quotation mark. So today’s date, and then I’m going to concatenate. All right, and then I’m just going to echo what I can actually just take each of these, I’ll just copy them since I already have them and concatenate on the day. Right? And then I can concatenate the slash. So I’m going to try and bring to just like this nine slash 21 slash 2019. So by that, if that’s what I want to do, then I really want one first, since it’s the nine first. So I have the month slash, then I concatenate. And then I’m going to concatenate the d value here. Alright, so that’s a Paul have been catenation. So we couldn’t print it out in one line just by seeing the 10 printing it did. Well, we can get the individual subscripts and then just concatenate it into one sentence on one structure that we want. All right. So that’s the sorry, that’s month, then they and then we’re going to know a colon the year so let me just this and then change that two year, some a cooling Oh, and I don’t need to echo each time. Sorry about that. So this one echo can serve this entire block. So that’s why we’re getting those are at nine. So I’m just going to remove all the extra echoes. All right, there we go. So Echo, and we’re echoing this one string attached to this next string. That’s this next one. What I didn’t do between The month and that will the D and the year is put in the slash. So just make sure I have in that slash here. So I have month the dot. And what I should have is the string to have the slash, and then concatenate that onto my year. And then and all of that with a semicolon. And then when I refresh, I’m going to see today’s date is the 21st of the ninth 2019. So based on your country, based on your formats, you can always arrange that so actually, I’m Jamaican. So in Jamaica, we prefer to have a month here as opposed to the month the year right. So I can just rearrange this by changing this to MD and then changing this to month and the year. And then when I refresh it looks more friendly to my cultural context. So you make your decision based on your audience. Though if I wanted the time by itself, then I could actually just take call. And I think I can just say time. All right, and let’s see how that works out. And there we go. Well, time is going to come by in some Unix value. So let me let me just break line here. So let me just concatenate a br tag here. And we look at the time value by itself. So that’s the time value. And that’s what you call like a Unix, you’d say it’s a Unix value. So it will make very little sense if you’ve never really used a Unix system. So you can see that this is not very human friendly when it comes to the time however, get dates actually has named subscripts that actually allow you to see the time so you can see ours just the same way you would type year mon r m. You can see hours, you can see minutes, you can say seconds, if you need to see an exact timestamp. Once again, this is one of those things that you probably won’t commit to memory. And remember everywhere you go every day of the week, but just something to bear in mind, because when you need it, you need to know exactly how you can implement it. And what your options are, though, in addition to those options with the get date function, you also have a date function that can give you a formatted date. So you just see m four, one, D, four d, y four year, alright, and whatever formats whether you want slashes or dashes or spaces, and then you can actually just put in something for that timestamp. And you would be doing all of that on top of time. Alright, so time is actually like a value since the beginning of computers, at least since it could be calculated. So this value can actually mathematically be broken down into the monster Year and time value, as at No. All right. So when I do that, and I print out, I notice I’m using our word print. So I’ll punch in though we’ve been echoing, you can also say print. Alright, so print and say date, and then passing that time value. And what it does is format, this time value, the seemingly unreadable time value into a readable format as you desire. And what it does is given me the month, the day, the year, and well I’m not sure why my time is being 23. When it’s really, it’s really 21. All right, but it seemed 23 and 18 and 49 seconds, so it’s 2380. And I guess maybe Messer or clock is wrong, maybe this representation. I think that needs to change. Something exam but you probably won’t be having that challenge. So that’s fine. And then in the next line, I’m actually printing something else to be is and I’m printing the day of the month, the year and a bunch of things, and he can see the translation here. So I’m seeing today is that day that year off September 2019, at anything 11:18pm. So you can just, instead of saying 2300 or 23 hours, which is the army time for 11 a, we’ll just say 11:18pm. That concludes today’s lesson on date and time manipulations. Of course, it can get more interesting than this. But as you develop into a PHP developer, you’ll find more use for these functions.