W3docs

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 returns false.
  • $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.

CodeReturnsExample
YFour-digit year2023
yTwo-digit year23
mMonth number (1–12)3
nMonth number (same as m)3
dDay of the month (1–31)5
jDay of the month (same as d)5
HHour, 24-hour (0–23)17
GHour, 24-hour (same as H)17
iMinutes (0–59)30
sSeconds (0–59)45
NISO day of week (1 = Monday … 7 = Sunday)7
wDay of week (0 = Sunday … 6 = Saturday)0
zDay of the year, zero-based (0–365)63
tNumber of days in the given month31
LLeap year? 1 if yes, 0 if no0
UThe Unix timestamp itself1678037445

Important: idate() returns a number, not a zero-padded string. idate('m') for March returns the integer 3, not "03". If you need a leading zero, use date('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"; // 45

Because 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 lengthOne character onlyAny number of characters
Return typeint (or false)string
Leading zerosNever (3)Yes, where defined ("03")
Use caseNeed a number for math/comparisonNeed 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 returns false. Call idate() once per component, or use date().
  • No leading zeros. Don't rely on idate() for fixed-width output; it always strips padding.
  • false looks like 0. On failure idate() returns false, which is loosely equal to 0. Use a strict check (=== false) if a real 0 (such as a Sunday from w) is a valid result.
  • 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.

Practice

Practice

Practice
What does the idate() function in PHP do?
What does the idate() function in PHP do?
Was this page helpful?