 Hey everybody, this is Brian and welcome to the 21st Lamp Tutorial. Today we're going to be discussing the date function. Alright, so working with dates and times can be a little complex, but fortunately PHP makes it very simple and very easy. The date function is literally date, format, timestamp, and the timestamp is optional. So let's just forget about the timestamp for right now. I'm going to cover that here in just a minute. The format is what we really need to focus on. And as you can see here, they have a little sample code, echo date, y, m, and d. Well, you probably guessed y stands for year, month stands for month, and d stands for day. And then the different formats you can put things in. Now, let's actually grab this and play around a little bit. Alright, so there's the different formats we have. I'm going to actually add this in here. And let's say we want to change this around. We want to do the day. Let's do some spaces here. F, and then what's that do? Sunday, November, 2012. Notice how the F is the full month. So there's a whole article out there where you can grab and see what letters do different things. But really, all you really need to know is the month, the day, and the year, if you're doing internal processing. If you want to make something look pretty, that's when you grab the D and the F, things of that nature. Now, one thing you should know is that there is a difference between upper and lower case. For example, let's say month, day, and year. And then let's print this out again with capital, month, day, and year. Let's see how these are different. Well, the lower case prints out just the numbers, where the upper case prints out the names. So there you have it. Let's go back to our trusty web browser here. Adding a timestamp. What's a timestamp? Remember that optional parameter we were talking about? Timestamp. Specifies the timestamp. The default is the current date and time. Well, a timestamp contains the number of seconds between the current time. I'm sorry, the time specified and the UNIX epoch, which is January 1, 1970, 000 GMT. What is that? If you come from a Windows world, you might be confused what that is. That is what the UNIX considers the start of time as we know it. So that's when the world started, according to UNIX, was in 1970. Now, the function we're going to use is MKTime, or as you guessed, MakeTime. And its format or parameters are hour, minute, second, month, day, year, and isDST, which is optional. And you can see down here is an example where we have tomorrow equal, MakeTime, 0 for the hour, 0 for the minute, 0 for the second. And then we're calling for the month, we're actually calling the date function. And we're just grabbing the month parameter of the current date. Same thing with the day, we're calling the day of the current date, and we're adding one because it's tomorrow, get it. And then for the year, just grabbing the year. Then we're echoing out tomorrow is, et cetera, et cetera. So let's try that out. Let's see how that works. Alright, let's add in another little hard return here so we know what's going on. So it's saying tomorrow is, and then, yep, sure enough, that's tomorrow's date. All we're doing is we're adding. That's how you make a timestamp, we're making time. So let's say you want to know what 30 days from now is. Just add 30, and then that's what 30 days from now would be here. And let's say you want to see what it's going to look like 25 years into the future. 2037, and one day, of course, because we left that in there. Pretty neat, huh? Working with time is actually pretty easy in PHP. Now, question always comes up, well, how do I compare two dates? Like, let's say you want to make a website that has some sort of subscription model and you want to make sure that somebody can go in and 30 days later they expire. Well, now comes the power of Google. Very simple. PHP compared dates. Just search Google and you'll find one of the pages out there has a really good explanation from highlystructured.com called Comparing Dates in PHP. And really, you should go out and read the full article. It explains it much better than I probably could. But he has some sample code out here that works pretty brilliantly. Expiration date, and then you give it whatever that is, and you can actually go in and do this, you know, current date plus whatever your subscription model would be, in this case, say 30. And then today's date. And then you do the str-to-time. That gets the Unix time stamp, I believe. And then you get the Unix time stamp of the expiration date, and then you simply compare the two. So let's actually play with this a little bit. It's always fun to play around, isn't it? Ooh, didn't like that. Let's see if g-edit won't work. Didn't like that. Well, let me type this in and then I'll restart the video. Sometimes I'm not as bright as I think I am. I can just, in Komodo IDE, I can just right-click and then paste HTML. And ta-da, there it is. So let's actually write this out. Compare dates. That's pretty embarrassing when you can't spell the word compare. There we go. And it says if and g-t. That's the greater than sign. That's the problem with the pasting HTML. Is it puts the HTML tag in there? There we go. Now we just want to echo out if this is valid. Is account valid? No. Why? Because we're measuring off 2006. Let's grab our tomorrow here. Well, let's just override this and we'll say, the expiration date actually equals tomorrow. Let's just comment this code out. Is it valid? No. Because we're saying is the expiration date greater than today? That should work. Okay, I see what I goofed up here. I'll just show you here. Running out of recording time. If we check the date, this case is 2006. So let's say 2012 because today is November 4th. So the account is suddenly valid because that's today. Let's say tomorrow because I did greater than or equal to. And then let's say three. Suddenly no. It's not because of the date. Because we're saying basically instead of valid, we should say is this expired? So we could take this and say, is expiration date less than today? Yes. If we say fifth, suddenly no. It's not valid. So that's a very simple example of how to compare dates. And let's kind of walk through this real quick here. Basically what they're doing is they're taking two dates. In this case, an expiration time and today's date. They're doing the str to time. Basically turning these into Unix timestamps or numbers. This is just a very big number. Let's actually print one of these out here. Unix looks like. And notice how it says Unix looks like. And that's actually what it's doing is converting it to this big number. Numbers are much easier to work with than trying to parse a string and figure out what does what. So it's just converting these into two numbers. And then you can just simply do some simple operators on it. Is it less than, greater than equal to, equal than or greater than, et cetera, et cetera. So that's how you compare dates. Pretty simple, pretty easy. I have to give a shout out to whoever owns highlystructured.com. I have no idea who you are, but thank you for that excellent little snippet. I'm sorry, I horribly screwed it up while I was trying to explain it. Anyways, this is Brian. Thank you for watching. I hope you found this educational and entertaining.