idate()
Certainly! idate() is a built-in PHP function that allows you to extract specific information from a Unix timestamp. It stands for "integer date" and returns an
idate() is a built-in PHP function that extracts a single piece of date or time information from a Unix timestamp and returns it as an integer. The name stands for integer date. Unlike date(), which builds a formatted string from one or more format characters, idate() accepts exactly one format character and gives you back a plain number you can do math on immediately.
This page covers the syntax, the format characters idate() understands, runnable examples, how it differs from date(), and the gotchas to watch for.
Syntax
idate(string $format, ?int $timestamp = null): int|false$format— a single format character (see the table below). Passing a string longer than one character triggers a warning and returnsfalse.$timestamp— an optional Unix timestamp (seconds since January 1, 1970, UTC). If omitted, the current date and time is used.
It returns an integer, or false on failure.
Format characters
idate() supports a subset of the date() format characters — only those that produce a numeric value.
| Code | Returns | Example |
|---|---|---|
Y | Four-digit year | 2023 |
y | Two-digit year | 23 |
m | Month number (1–12) | 3 |
n | Month number (same as m) | 3 |
d | Day of the month (1–31) | 5 |
j | Day of the month (same as d) | 5 |
H | Hour, 24-hour (0–23) | 17 |
G | Hour, 24-hour (same as H) | 17 |
i | Minutes (0–59) | 30 |
s | Seconds (0–59) | 45 |
N | ISO day of week (1 = Monday … 7 = Sunday) | 7 |
w | Day of week (0 = Sunday … 6 = Saturday) | 0 |
z | Day of the year, zero-based (0–365) | 63 |
t | Number of days in the given month | 31 |
L | Leap year? 1 if yes, 0 if no | 0 |
U | The Unix timestamp itself | 1678037445 |
Important:
idate()returns a number, not a zero-padded string.idate('m')for March returns the integer3, not"03". If you need a leading zero, usedate('m')instead, which returns the string"03".
Basic example
<?php
// March 5, 2023, 17:30:45
$timestamp = mktime(17, 30, 45, 3, 5, 2023);
echo idate('Y', $timestamp) . "\n"; // 2023
echo idate('m', $timestamp) . "\n"; // 3
echo idate('d', $timestamp) . "\n"; // 5
echo idate('H', $timestamp) . "\n"; // 17
echo idate('i', $timestamp) . "\n"; // 30
echo idate('s', $timestamp) . "\n"; // 45Because the return value is an integer, you can use it directly in arithmetic and comparisons:
<?php
$timestamp = mktime(17, 30, 45, 3, 5, 2023);
if (idate('N', $timestamp) >= 6) {
echo "It's the weekend.\n";
} else {
echo "It's a weekday.\n";
}
// March 5, 2023 was a Sunday (N = 7), so this prints:
// It's the weekend.Using the current time
When you omit the second argument, idate() reads the current date and time, which makes it handy for quick checks:
<?php
$currentYear = idate('Y');
echo "The current year is {$currentYear}.\n";
// Detect a leap year
echo idate('L') ? "This year is a leap year.\n" : "This year is not a leap year.\n";idate() vs. date()
idate() | date() | |
|---|---|---|
| Format length | One character only | Any number of characters |
| Return type | int (or false) | string |
| Leading zeros | Never (3) | Yes, where defined ("03") |
| Use case | Need a number for math/comparison | Need a formatted, human-readable string |
<?php
$timestamp = mktime(9, 5, 0, 4, 2, 2024);
echo date('m', $timestamp) . "\n"; // "04" (string, zero-padded)
echo idate('m', $timestamp) . "\n"; // 4 (integer, no padding)When to use it
Reach for idate() when you want a date/time component as a number — for example to build a counter, compare two values, index into an array, or feed the result into further calculations. If your goal is to display a formatted date or time, use date() (or strtotime() to parse text into a timestamp first).
Gotchas
- No multi-character formats.
idate('Y-m-d')does not work — it warns and returnsfalse. Callidate()once per component, or usedate(). - No leading zeros. Don't rely on
idate()for fixed-width output; it always strips padding. falselooks like0. On failureidate()returnsfalse, which is loosely equal to0. Use a strict check (=== false) if a real0(such as a Sunday fromw) is a valid result.
Related functions
date()— format a timestamp as a string.mktime()— build a Unix timestamp from date/time parts.strtotime()— parse an English date string into a timestamp.time()— get the current Unix timestamp.