Dealing with dates and times is a common, but often tricky, requirement in web development. You need to handle user time zones, calculate time differences, and format timestamps into human-readable strings. PHP provides dedicated functions to handle all of this.
1. Setting the Timezone
Before working with dates, you must tell PHP which timezone your script should use. If this is not set, PHP might issue a warning and use the server’s default timezone, which can lead to errors.
You set the timezone using the date_default_timezone_set() function.
Example:
<?php
// Set the timezone to an appropriate value (e.g., your local timezone)
date_default_timezone_set("Asia/Kolkata");
// Or for the US: date_default_timezone_set("America/New_York");
?>
2. The date() Function (Formatting)
The most important function is date(), which formats a timestamp (a large integer representing the seconds since January 1, 1970) into a human-readable date and time string.
The date() function requires a format string that uses special characters to define the output structure.
| Format Character | Description | Example Output |
Y | Full year (4 digits) | 2025 |
m | Month (01 to 12) | 11 |
d | Day of the month (01 to 31) | 30 |
H | Hour (00 to 23) | 13 |
i | Minutes (00 to 59) | 37 |
s | Seconds (00 to 59) | 02 |
l (lowercase L) | Full day of the week | Sunday |
Example: Displaying the Current Date and Time
<?php
date_default_timezone_set("Asia/Kolkata");
// Y-m-d H:i:s is a standard format for database timestamps
echo date("Y-m-d H:i:s") . "<br>";
// Output Example: 2025-11-30 13:37:02
// Displaying a custom, friendly format
echo "Today is " . date("l, F j, Y");
// Output Example: Today is Sunday, November 30, 2025
?>
3. The time() and mktime() Functions (Timestamps)
A timestamp is the number of seconds that have passed since January 1, 1970 (the Unix Epoch). PHP uses this internal format for all date calculations.
| Function | Purpose |
time() | Returns the current Unix timestamp (a large integer). |
mktime() | Creates a Unix timestamp from a specified date/time. |
Example: Getting and Comparing Timestamps
<?php
// 1. Get the current time as a simple integer
$currentTime = time();
echo "Current Timestamp: " . $currentTime . "<br>";
// 2. Create a timestamp for a specific future date (Dec 25, 2025, at 10:00:00)
// mktime(hour, minute, second, month, day, year)
$christmasTime = mktime(10, 0, 0, 12, 25, 2025);
// 3. Calculate the difference in seconds
$secondsUntilChristmas = $christmasTime - $currentTime;
// 4. Convert seconds to days
$days = round($secondsUntilChristmas / (60 * 60 * 24));
echo "Days until Christmas: " . $days . " days.";
?>
4. Converting a String to a Time
If you have a date stored as a string (e.g., from a form or database), the strtotime() function is used to convert it into a usable Unix timestamp.
| Syntax | Description |
strtotime(time_string) | Parses nearly any English date/time description into a timestamp. |
Example:
<?php
$db_date_string = "2024-05-10";
// Convert the string into a timestamp
$timestamp = strtotime($db_date_string);
// Now you can format it however you like
echo "Formatted: " . date("M jS, Y", $timestamp);
// Output: Formatted: May 10th, 2024
?>
Next Steps
You have now completed the core set of essential PHP building blocks. The next chapter should cover the missing foundational topic of Numbers and Math before we move into the crucial realm of Object-Oriented Programming (OOP).
