Menu
Coddy logo textTech

The 'date()' Function

Part of the Logic & Flow section of Coddy's PHP journey — lesson 46 of 68.

The date() function is PHP's primary tool for formatting dates and times into readable strings. It takes the current date and time and displays them according to format characters you specify, making it essential for showing dates to users in your applications.

The function uses single-letter format characters to represent different parts of a date or time. The most common ones include Y for a four-digit year, m for a two-digit month, d for a two-digit day, H for hours in 24-hour format, i for minutes, and s for seconds.

<?php
echo date("Y-m-d");  // Outputs: 2024-03-15
echo date("H:i:s");  // Outputs: 14:30:25
echo date("Y-m-d H:i:s");  // Outputs: 2024-03-15 14:30:25
?>

You can combine these format characters with any separators or text you need. The date() function automatically uses the current date and time when called, making it perfect for timestamps, logs, or displaying "last updated" information on your web pages.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow