Eastern Michigan University EMU HOME
 
people.emich.edu
About people.emich.edu
Tips & Resources
Print this page
people.emich.edu

Introduction to PHP: A Useful Example

Example 2: "Last Modified" Date

Let's say we want to display the date the document was last modified on the page. This can be accomplished using PHP as shown in this example.

I have uploaded a file called example2.php. Its contents are shown below.

	<html>
	   <head>
	      <title>Example 2</title>
	   </head>
	   <body>
	   This page was last modified
	   <?php 
	      // Print the date this script was last modified
	      echo date("l F j, Y", filemtime($_SERVER["SCRIPT_FILENAME"])); 
	   ?>.
	   </body>
	</html>

See Example 2.

Computer Science types will have no problem getting a hang of what is going on here. The script within the <?php ?> tag was constructed as follows. Please see the PHP Manual for more details about each function.

  • The echo command displays the string returned by date().
  • The date(format_string, time) function returns a string containing time returned by filemtime() formatted according to format_string.
  • The date format string "l F j, Y" breaks down to
    • l = Weekday
    • F = Month
    • j = Day of the month
    • Y = Year
  • The filemtime(filename) function returns the time the file whose name is contained by filename, in our case $_SERVER["SCRIPT_FILENAME"], was last modified.
  • The $_SERVER["SCRIPT_FILENAME"] variable contains the filename of the currently executing script.

Even if you don't really care how Example 2 works, you can copy and paste the <?php ?> tag into your PHP document to use it.

Next: Templates




EMU Home | Contact EMU | Site Map | Directories | Calendars | My.emich | Search