Join David Powers for an in-depth discussion in this video Displaying the current date and time, part of PHP Date and Time Essential Training.
- In this chapter, we'll look at basic date and time functions that have been inherited from PHP 4. Although they're not as sophisticated as the classes introduced in PHP 5, they're often all that you need. Let's begin by looking at how to display the current date or time. The PHP date function displays a local date or time using the server's default time zone. So it takes daylight saving into account when appropriate. The function takes two arguments. The first one, which is required, is a string and this formats the date.
The second one, which is optional, sets the time. If you omit the second argument, it defaults to the current date and time. In principle, it's very simple. What's not quite so simple is remembering the formatting options. They're listed here in the PHP online documentation. Let's scroll down and take a look at them. There are more than 30 formatting characters. Some are easy to remember, but most of them aren't. For example, lowercase d gives you the day of the month, 2 digits with leading zeros.
But if you don't want those leading zeros, you need to use a lowercase j. Uppercase D gives you the first three letters of the weekday name, so that's Monday through Sunday, or Mon through Sun. But if you want the full name, then you need to use a lowercase l. The names, by the way, are all in English only. Let's give that a little tryout. Let's go to my editing program, and we'll just have today's day.
So, echo. Then we'll concatenate that with the date function, and the formatting string will be a lowercase l. If we save that and then view it in the browser, it tells us that "Today is Monday." And of course, that will differ depending on which day of the week that you're testing this on. But today is Monday for me.
You can combine the formatting characters and use punctuation. Let's add a comma after the lowercase l, space, uppercase F, lowercase j, another comma, a space, and an uppercase Y. Save that and refresh the browser. And it tells us that "Today is Monday, "January 5, 2015." So an uppercase F is the full name of the month, lowercase j is the date without a leading zero, and uppercase Y is a four-digit year.
You display the time in the same way, using formatting characters. Let's add another line here. So, echo, "the time is" then we use the date function again, and our formatting string will have a lowercase g, followed by a colon, a lowercase i followed by a space and an a. And if we save that and refresh the browser, it tells us that "the time is 12:29 pm." Lowercase g gives you the hour in 12-hour format, and lowercase i gives you the minutes, and a lowercase a represents am or pm in lowercase.
We can also add seconds. That's a lowercase s. So we'll just add in another colon here and lowercase s, resave and refresh, and it shows us that it's "12:30:04." PHP is a server-side language. So the time is static. It doesn't update unless the page is reloaded in the browser. In the same way as a formatting string can contain punctuation, you can also add plain text.
Let's just try that. We'll take this out of here, copy that, and we'll just comment that line out. Then we'll paste this back in here and then we'll add "on." We're hoping to get the time on the particular date. We save that and refresh. But instead of "on," we get "20151." The problem is that both lowercase o and lowercase n are also formatting characters.
To get the plain text, you need to escape that. We escape with a back slash, so a back slash before o, back slash before n, resave and refresh. And this time, we get that actual text. The date function can also be used with predefined constants. They're all listed in the online documentation. Let's just go back to the documentation and, if we scroll up just a little bit, we can see that there is a link here to the predefined date constants.
If we follow that, here are the constants. There are two types of constant. The first one is DateTime:: and then the constant name, and then DATE_ followed by the constant name. They both do exactly the same thing. The first one is for the DateTime class. The second one is a PHP global constant. It doesn't matter which you use, they're all supported. If we use DATE_COOKIE, this will format the date for a cookie.
Let's just go back to the editing program and we'll comment that line out. Then we'll just echo the cookie. So echo, and then date, and then DATE_COOKIE, save that, go back to the browser and refresh. And there is the time formatted exactly as you would want it for a cookie. To display the current date or time, use the date function with a single argument, which should either be a string using formatting characters or a DateTime constant.
The most difficult aspect of working with the date function is trying to remember the formatting characters. So it's a good idea to bookmark the function's online documentation where they're all listed.
- How PHP handles dates and time
- Setting the default time zone
- Displaying current date and time
- Getting a timestamp from text
- Using the DateTime class
- Getting and modifying a timestamp from DateTime
- Comparing DateTime objects
- Working with time zones
- Using DateInterval and DatePeriod
- Finding ISO week dates
- Calculating sunrise and sunset