PHP Date

Working with dates and times in PHP

📅 What is PHP Date?

PHP date functions help you format and manipulate dates and times. The date() function converts timestamps into readable formats for displaying on web pages.


<?php
// Display current date
echo date("Y-m-d");
// Outputs: 2025-03-10
?>
                                    

Output:

2025-03-10

Date Format Characters

📆

Day Formats

Display day in various formats

<?php
echo date("d"); // 01-31
echo date("D"); // Mon-Sun
?>
📅

Month Formats

Display month in different ways

<?php
echo date("m"); // 01-12
echo date("M"); // Jan-Dec
?>
🗓️

Year Formats

Display year formats

<?php
echo date("Y"); // 2025
echo date("y"); // 25
?>

Time Formats

Display time in various formats

<?php
echo date("H:i:s"); // 14:30:45
echo date("h:i A"); // 02:30 PM
?>

🔹 Basic Date Function

The date() function formats a timestamp into a readable date string. Use format characters to customize the output according to your needs.

<?php
// Current date and time
echo "Today is: " . date("Y-m-d") . "<br>";
echo "Current time: " . date("H:i:s") . "<br>";
echo "Full date: " . date("l, F j, Y") . "<br>";
echo "Short format: " . date("m/d/Y") . "<br>";
?>

Output:

Today is: 2025-03-10
Current time: 14:30:45
Full date: Monday, March 10, 2025
Short format: 03/10/2025

🔹 Timestamp Functions

Timestamps represent dates as seconds since January 1, 1970. Use time() to get current timestamp and mktime() to create custom timestamps.

<?php
// Current timestamp
$now = time();
echo "Current timestamp: " . $now . "<br>";

// Create specific date timestamp
// mktime(hour, minute, second, month, day, year)
$birthday = mktime(0, 0, 0, 5, 15, 1990);
echo "Birthday timestamp: " . $birthday . "<br>";

// Format timestamp
echo "Birthday: " . date("F j, Y", $birthday);
?>

Output:

Current timestamp: 1741615845
Birthday timestamp: 642643200
Birthday: May 15, 1990

🔹 String to Date Conversion

Convert date strings to timestamps using strtotime(). This function parses English text descriptions of dates into Unix timestamps for calculations.

<?php
// Convert string to timestamp
$date1 = strtotime("2025-12-25");
echo "Christmas: " . date("l, F j", $date1) . "<br>";

// Relative dates
echo "Tomorrow: " . date("Y-m-d", strtotime("tomorrow")) . "<br>";
echo "Next week: " . date("Y-m-d", strtotime("+1 week")) . "<br>";
echo "Last month: " . date("Y-m-d", strtotime("-1 month")) . "<br>";
?>

Output:

Christmas: Thursday, December 25
Tomorrow: 2025-03-11
Next week: 2025-03-17
Last month: 2025-02-10

🔹 Date Calculations

Perform date arithmetic by converting dates to timestamps, doing calculations, and converting back. This helps calculate differences and future dates.

<?php
// Calculate days until event
$today = time();
$event = strtotime("2025-12-31");
$diff = $event - $today;
$days = floor($diff / (60 * 60 * 24));

echo "Days until New Year: " . $days . "<br>";

// Add days to date
$futureDate = strtotime("+30 days");
echo "30 days from now: " . date("Y-m-d", $futureDate) . "<br>";

// Calculate age
$birthdate = strtotime("1990-05-15");
$age = date("Y") - date("Y", $birthdate);
echo "Age: " . $age . " years";
?>

Output:

Days until New Year: 296
30 days from now: 2025-04-09
Age: 35 years

🔹 Common Date Formats

Here are frequently used date format patterns for different display needs. Mix and match format characters to create custom date strings.

<?php
$timestamp = time();

// Common formats
echo date("Y-m-d", $timestamp) . "<br>";           // 2025-03-10
echo date("d/m/Y", $timestamp) . "<br>";           // 10/03/2025
echo date("F j, Y", $timestamp) . "<br>";          // March 10, 2025
echo date("l, F j, Y", $timestamp) . "<br>";      // Monday, March 10, 2025
echo date("D, M j, Y", $timestamp) . "<br>";      // Mon, Mar 10, 2025
echo date("Y-m-d H:i:s", $timestamp) . "<br>";    // 2025-03-10 14:30:45
echo date("h:i A", $timestamp) . "<br>";          // 02:30 PM
?>

Output:

2025-03-10
10/03/2025
March 10, 2025
Monday, March 10, 2025
Mon, Mar 10, 2025
2025-03-10 14:30:45
02:30 PM

🧠 Test Your Knowledge

Which function returns the current Unix timestamp?