idate
Formats a Unix timestamp with a single format character and returns the result as an integer.
idate(string $format, ?int $timestamp = null): int|false- Added in PHP 5.0.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$format | string | A single format character (for example Y, m, d, H, i, s, t, L, N, o, W, z, U, Z). Multi-character formats are not accepted. |
$timestampoptional | ?int= null | The Unix timestamp to format. Defaults to the current local time when omitted or null. |
Return value
int|falseAn integer representing the requested date part, or false on failure.
Errors
Emits E_WARNING if the default time zone is not valid; returns false when the format character is not one of the supported single characters.
Changelog
- PHP 8.2.0Added the N (ISO-8601 day of the week) and o (ISO-8601 year) format characters.
- PHP 8.0.0timestamp is nullable now.
idate() is date() narrowed to one job: give it exactly one format character and it hands back an int instead of a string. That matters when the value is going straight into arithmetic or a strict comparison.
When to reach for it
Use it when you need a date part as a number and would otherwise write (int) date('m', $ts). Typical cases: bucketing rows by month number, checking idate('L', $ts) === 1 for a leap year, or reading idate('t', $ts) to find how many days a month has before building a calendar grid.
What trips people up
Because the return is an integer, leading zeros are gone. date('m') in March gives the string "03"; idate('m') gives 3. For a two-digit year, date('y') gives "04" for 2004 while idate('y') gives 4. Never use idate() to build a displayed date.
Only a subset of date()'s characters work — anything textual (D, l, M, F) has no integer meaning and is rejected. And false is a legitimate return value, so test it with === rather than a truthiness check, since 0 is also a perfectly valid result for L, I or w.
Example
When to use it
- Group log rows by month number without casting date() output to int
- Detect a leap year or read the number of days in a month before rendering a calendar
- Compare the ISO week number of two timestamps in a strict === check
Frequently asked questions
What is the difference between idate and date?
idate returns an int and accepts only one format character, while date returns a string and accepts a full format template. Use idate when the value feeds arithmetic or a strict comparison, and date whenever the value is displayed.
Why does idate('m') return 3 instead of 03?
The return type is int, and integers have no leading zeros. Use date('m') or str_pad if you need the zero-padded form.
How do I check whether idate failed?
Compare with === false. A plain falsy test is wrong because 0 is a valid result for format characters like L, I and w.
More examples
Integer month arithmetic
The month comes back as 11 rather than "11", so intdiv() works on it directly with no cast. Output is month: 11, quarter: Q4, days in month: 30.
<?php
date_default_timezone_set('UTC');
$ts = strtotime('2025-11-20');
$month = idate('m', $ts);
$quarter = intdiv($month - 1, 3) + 1;
echo "month: $month\n";
echo "quarter: Q$quarter\n";
echo "days in month: ", idate('t', $ts), "\n";ISO-8601 week, year and weekday
The o and N characters were added in PHP 8.2. Around New Year the ISO year can differ from the calendar year, which is why reporting code should use o with W rather than Y.
<?php
date_default_timezone_set('UTC');
$ts = strtotime('2026-01-01'); // a Thursday
echo 'calendar year: ', idate('Y', $ts), "\n";
echo 'ISO year: ', idate('o', $ts), "\n";
echo 'ISO week: ', idate('W', $ts), "\n";
echo 'ISO weekday: ', idate('N', $ts), "\n"; // 1 = Monday
Discussion