Today's Date Using PHP

Display the Current Date on Your Website

Young businessman sitting on couch and working on laptop
Neustockimages/E+/Getty Images

Server-side PHP scripting gives web developers the ability to add features that change to their websites. They can use it to generate dynamic page content, collect form data, send and receive cookies and display the current date. This code only works on pages where PHP is enabled, which means the code displays a date on pages that end in .php. You can name your ​HTML page with a .php extension or other extensions set up on your server to run PHP.

Example PHP Code for Today's Date

Using PHP, you can display the current date on your website using a single line of PHP code.

Here's How It Works

  1. Inside an HTML file, somewhere in the body of the HTML, the script starts by opening the PHP code with the symbol.
  2. Next, the code uses the print( ) function to send the date it is about to generate to the browser.
  3. The date function is then used to generate the current day's date.
  4. Finally, the PHP script is closed using the ?> symbols.
  5. The code returns to the body of the HTML file.

About That Funny-Looking Date Format

PHP uses formatting options to format date output. The lower case "L"—or l—represents the day of the week Sunday through Saturday. F calls for a textual representation of a month such as January. The day of the month is indicated by d, and Y is the representation for a year, such as 2017. Other formatting parameters can be seen at the PHP website.

mla apa chicago
Your Citation
Bradley, Angela. "Today's Date Using PHP." ThoughtCo, Aug. 20, 2017, Bradley, Angela. (2017, August 20). Today's Date Using PHP. Retrieved from Bradley, Angela. "Today's Date Using PHP." ThoughtCo. (accessed March 25, 2018).