Become a better PHP programmer, one tutorial at a time. Broaden your understanding of what PHP can do and become a more productive developer, with these weekly tips.
- Hi, I'm David Powers and welcome to this week's edition of PHP Tips, Tricks, and Techniques, designed to help you become a smarter, more productive PHP developer. This week I'm going to look at setting a future date using PHP. This can be useful in several ways. For example, setting the date someone's membership expires. It's also useful for finding the date of regular events, such as the second Tuesday in a month. PHP makes setting a future date remarkably easy.
There are several ways to do it. I'm going to use the built-in date-time class. By the way, I'm starting with a blank file, so the exercise files for this video contain only the finished code. We need to begin by creating a variable for the date, we'll call that simply date, and then an instance of the date-time class. Without passing any arguments to the date-time constructor, this creates a date-time object for the current date and time.
To create a future date, we can pass the constructor a string containing a relative date. For example, if you're storing membership details in the database and you want the membership to expire in 12 months, we just create a string, plus 12 months. So, pair of quotes, plus, and the number of months. To insert the value in the database or to display it on screen, we need to use the date-time class's format method. And we can call it directly by wrapping this instance in a pair of parentheses like this.
Then we can have the arrow operator and invoke the format method, this requires a formatting string, to insert the date into MySQL on the rear DB, it needs to be in the order year, month, date, so uppercase Y, hyphen M, hyphen D. And we can inspect that by using echo. So if I save that, and run the script by loading it into a browser, it gives me this result.
I'm recording this in late November, 2017, and it shows me the correct date, 12 months from now. The 29th of November, 2018. Obviously, you'll get a different date when you try it yourself. If it doesn't work, try removing this space between the plus and the numeral. I seem to remember that older versions of PHP don't accept that space. Now let's say you want to coordinate renewal dates at the end of the month. No problem, PHP relative date strings are very powerful, and can understand a wide range of simple expressions.
So let's just duplicate that line, then we'll comment out previous one, and then we'll update this relative date string. So instead of just plus 12 months, we'll have last day of this month, plus 12 months, and if we save that, at the moment, we're getting the 29th of November, let's just update that, now we get the 30th of November, and of course, that date will depend on the number of days in the month.
Let's say I'm giving someone a three-month membership. Let's just duplicate that line, comment out the previous one, and change this to three months, save and refresh, and it tells me the 28th of February, 2018. That's correct, because three months from now will be the last day of February. It's also aware of leap years. At the time of recording, I need to add 27 months to get the next leap year, so if we go through that process again, instead of three months, 27 months, and update that, we get the 29th of February, 2020.
Pretty smart, eh? Once you've got the future date that you need, you can use it however you like. If you're creating a membership subscription system, you drop it into an SQL query to insert into your database or to update an existing record. Just remember that if you're inserting it into a date or a date-time column, it's a string, so it needs to be wrapped in quotes. Another really useful feature of relative date strings is you can use them to find the date of a regularly occurring event. For example, in Canada, Thanksgiving is always the second Monday in October.
So we can find the date for Canadian Thanksgiving in 2018. Let's just comment out both of those lines, and then Thanksgiving, and it's the Canadian one, so we'll have ca on the end there and we need a date-time object, and then the relative date string, so it's going to be the second Monday of October, 2018.
And again, we need to format that, so put that in a pair of parentheses. And we'll echo it, save that, and refresh the browser, and it tells us that the eighth of October, 2018, will be Thanksgiving in Canada in 2018.
Now, notice that I have used the second Monday of October. If I change that to in October, save and refresh, I get a fatal error, because PHP can't understand in October, it needs to be of October. So now I've changed that back, everything is working correctly again. And of course, if you're in America, all you need to do is change this to the fourth Thursday of November, 2018, and you'll get the date of American Thanksgiving.
You can also use relative date strings to work out a series of dates, such as the third Thursday of each month. Check out chapter five of my PHP Date and Time Essential Training in the online library for details of how to do it. To learn more about the relative date format supported by PHP, visit this page in the PHP online documentation, and pay careful attention to the syntax, and test your results before deploying your script in production. You saw that problem that we had with the difference between in and of, and to output your dates, you need to use a formatting string, the details of that are in this page of the online documentation, if we just scroll down, there are all the characters that are recognized in the format parameter string, the format method of the date-time class uses exactly the same formatting strings as the date function in PHP.
Well, that's it for this week's PHP Tips, Tricks, and Techniques, I hope you'll join me again next week, thanks for watching.
Note: The exercise files are free to all members. The code is commented to enhance your learning, but you will need database connectivity for some files to run as intended.