Let PHP Do the Work5:34 with Alena Holligan
PHP has many built-in functions that can handle complex programming tasks for us. Let PHP work for you by utilizing built-in functionality. We'll be using the date function that we used in the Daily Exercise Program to update copyright dates.
Creating a Timestamp
getlastmod() - Gets timestamp of last page modification of current page
filemtime() - Allows you to get the timestamp of the last modification date of a specified file.
mktime() - Allows you to get the timestamp for a specific date and time
strtotime() - Parse about any English textual datetime description into a timestamp
According to Larry Wall, the original author of the Perl programming language, there are three great virtues of a programmer; Laziness, Impatience and Hubris
Laziness: The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful and document what you wrote so you don't have to answer so many questions about it.
Impatience: The anger you feel when the computer is being lazy. This makes you write programs that don't just react to your needs, but actually anticipate them. Or at least pretend to.
Hubris: The quality that makes you write (and maintain) programs that other people won't want to say bad things about.
Quoted from "Programming Perl", 2nd Edition, O'Reilly & Associates, 1996 http://threevirtues.com/
We've seen how we can use variables with PHP to make updates faster. 0:00 Now let's get PHP to make some actual updates for us. 0:05 PHP has many built in functions that can be used to handle complex programming 0:08 tasks. 0:13 How long after the New Year do you keep writing the previous year for dates? 0:15 I'm usually good in January but then start slipping again in February. 0:19 And who wants to spend their first week of the New Year updating copyright dates 0:22 in every footer on every web page? 0:26 Instead, let PHP do it for you. 0:28 We'll be using the date function that we used in the daily exercise program. 0:31 I've included a link to the documentation in the teacher's notes. 0:35 You've already added code blocks to this page, so 0:39 you have all the tools that you need. 0:41 Pause this video and see if you can change the year 0:43 in the footer to have PHP automatically update that for us. 0:46 Go ahead I'll wait right here. 0:51 So, did you figure it out? 0:53 Don't worry if you didn't quite get there this time, 0:55 I'll take you through each step right now. 0:58 Down here in the footer, 1:01 we want to replace the year 2016 with the PHP date function. 1:03 To start, we replace the year with the PHP code block. 1:07 The next step is to add our function call. 1:15 This time we want the date function to return the four digit year, so 1:20 we pass the capital Y in quotes to the date function. 1:24 Check the teacher's notes for a link to the docs with all the options available. 1:28 For controlling how the date function outputs date information. 1:32 Let's save the file and refresh the browser. 1:35 Now the year is gone completely but there's no errors, so what went wrong. 1:39 Let's go back to work spaces. 1:43 The call to the function is good but it only returns the value, 1:45 we haven't told the script what we want to do with the value. 1:49 For our daily exercise program we assign the value to a variable called Today. 1:53 This time we just want to display the value, 1:58 so let's add the echo command before the date. 2:01 Now we can save and refresh our browser again. 2:04 Great, this time we see the current year. 2:09 Awesome, now we don't have to worry about updating that date for the new year. 2:13 But PHP provides even more built in functions that we can use, 2:17 sometimes the page may also contain our last modified date. 2:21 This can be helpful for 2:25 information that is time sensitive such as news or a report. 2:26 But I don't wanna have to rely on my memory to update that last modified date, 2:30 we could be in trouble if we did. 2:34 PHP to the rescue, let's pull up the docs for the date function. 2:36 In the docs for 2:42 the date function, we can see that we can pass a second optional parameter as well. 2:43 By default it uses the current timestamp this moment in time. 2:48 But we can also specify a timestamp if we want to use a different date or time. 2:52 There are many different ways to use PHP to create a timestamp, 2:57 let's scroll down to the see also heading. 3:00 For this solution, I want to get the timestamp of the last modified date 3:05 to do that we use a function call getlastmod. 3:09 Getlastmod, gets the time of the last modification of the current page. 3:14 Since a function returns a value, 3:19 I can use a function within another function to supply a value. 3:21 If you scroll down you can see the first example on the getlastmod page. 3:25 Let's copy this code block and use it in our footer as well. 3:31 We can also combine these code blocks, so that it's all one code block. 3:39 Combining code blocks when there's no HTML separating them can once again make things 3:42 easier to read. 3:47 Now let's save this page and refresh our browser. 3:56 So, now we see last modified with a month, day, year and time. 4:01 When we refresh the page, 4:06 the last modified date doesn't change because we have not resaved the file. 4:07 We should put some space separating the name and the last word. 4:12 So let's modify the file again and add a period and a space. 4:16 Let's add some things here. 4:21 We wanna space after the year and 4:23 we'll and a period and a space at the end. 4:27 This time when we refresh the browser, we see a period and a space. 4:34 We also see that the time did change because we made changes to the file. 4:39 Nice work, you're learning to make use of one of the programmers greatest strengths, 4:44 laziness. 4:49 According to Larry Wall author of the Perl Programming Language, 4:51 laziness is one of the greatest virtues of a programmer. 4:54 Because it makes you go through great efforts to reduce how much energy you use. 4:57 Laziness makes you write labor saving programs that other people find useful. 5:03 And if you're lazy, you won't wanna answer a lot of questions about your program. 5:07 So, make sure that you document your code well. 5:12 Lazy or not, reducing the amount of busy work I need to do. 5:15 Gives me more time to do the ever increasing list of things that 5:18 I want to accomplish. 5:21 Besides, it sure is rewarding to build something that's actually useful. 5:23 Let's finish off this course by combining the unit converter and 5:27 the daily exercise programs into this page as well. 5:30
You need to sign up for Treehouse in order to download course files.Sign up