← Back to Chapters

PHP Date Function

? PHP Date Function

? Quick Overview

The date() function in PHP formats dates and times into readable strings using format characters. It can work with the current system time or with a custom Unix timestamp.

? Key Concepts

  • Formats dates using format characters
  • Uses Unix timestamps internally
  • Supports custom time values

? Syntax / Theory

? View Code Example
// date() formats a timestamp into a readable date string
date(format, timestamp);

? Code Example 1: Default Timestamp

? View Code Example
// Displays current date and time

? Output Explanation

The current system date and time are formatted as Year-Month-Day Hour:Minute:Second.

? Code Example 2: Custom Timestamp

? View Code Example
// Formats a custom timestamp

? Output Explanation

The timestamp represents Christmas Day 2025 at noon and is formatted accordingly.

? Interactive Example

Click the button to see today’s date using JavaScript formatting.

? View Code Example
// Shows today's date dynamically
document.getElementById("showDate").onclick = () => {
document.getElementById("liveDate").textContent = new Date().toDateString();
};

 

? Use Cases

  • Displaying blog post dates
  • Logging events and errors
  • Formatting user-friendly timestamps

✅ Tips & Best Practices

  • Always set the correct timezone using date_default_timezone_set()
  • Use meaningful date formats for users
  • Combine with strtotime() for flexibility

? Try It Yourself

  • Display the date as d-m-Y
  • Show the day name using l
  • Convert user input into a formatted date