Join David Powers for an in-depth discussion in this video Using the exercise files, part of PHP Date and Time Essential Training.
- If you're a subscriber to the Lynda.com online training library you have access to the exercise files for this course. Just download them from the link on the course's page and unzip them to a convenient location. I've got them here on my desktop. And if we open that folder you can see that all the files are organized in folders corresponding to each chapter. Because we're working with PHP, you need to copy the exercise files into your server root. I'm using a local testing service. I've copied them into a folder called, datetime in my server's Htdocs folder.
Let's just open my editing program so we can see more easily how the files are organized. If we open one of those chapters, let's say chapter four, inside there is a separate folder for each video that uses exercise files. If I open one of those folders, this contains all of the files that you need for that particular video. In this case there are two files that are in the state at the beginning of the video, offset.php and time_difference.php.
And then the versions with "end" appended to the file name show what the file looks like at the end of the video. This folder also contains a text file which has got some snippets of code for you to paste into one of the main files. In some cases, there is no file with "end" appended to the file name. One example is here in 04_01, abbreviations.php. I begin that video with a blank file. So abbreviations.php is what it looks like at the end of the video.
The other type of file that doesn't have "end" appended to it. There's an example in 04_07 down here. Clock_change.php. If we just open that. This is an example file that I don't build step by step but I just explain how it works. And with this sort of file, the code is fully commented. So you can read these comments at your leisure to understand the logic of the code. Each video is complete in itself. And that means you can dip into the course to find out about a specific topic that interests you.
But having said that, the course does follow a logical progression. You might be tempted, for example, to go straight to chapter five to find out about generating recurring series of dates. But to do that, you'll need to know how to use the date time and date time zone objects. Which are the subjects of chapter three and chapter four. Similarly, chapter seven deals with calculating sunrise and sunset, but it draws on subjects that are discussed in most earlier chapters.
All of the files require a minimum of PHP 5.4. But there's one file that does require 5.5. That's in this folder, chapter three, 03_12. And it's immutable.php. And this uses the datetime immutable class which was introduced only in PHP 5.5. So now you know how the files are organized. Let's start our exploration of working with dates and time in PHP.
- 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
Skill Level Intermediate
Q: How does calculating sunrise and sunset (as shown in chapter 7) work for locations north of the Arctic Circle?
A: In polar regions, date_sun_info() reports the value for sunrise and sunset as 1 on days when the sun never sets. The values are empty for days when the sun never rises.