PHP idate() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

In PHP, idate() returns one part of a date or time as an integer, such as the year, month, day or hour.

You pass a single format character and a timestamp. Unlike date(), which returns a string like '05', idate() returns a real number like 5, so you can use it directly in math.

Syntax

Call idate() with the values you need. Start with this line.

PHP
$year = idate('Y', 1700000000);
CallWhat it does
idate('Y', 1700000000)Returns the integer 2023.
idate('m', 1700000000)Returns the integer 11.
idate('H', 1700000000)Returns the integer 22.

The format must be exactly one character. Common ones are Y (year), y (two-digit year), m (month), d (day), H (hour 0-23), i (minutes), s (seconds), w (weekday, 0 = Sunday), z (day of year, 0-based), t (days in the month) and L (1 for a leap year).

Library declarations
PHP
idate(string $format, ?int $timestamp = null): int|false

Parameters

ParameterDescription
formatRequired. One character that says which part to return. A longer string throws a ValueError.
timestampOptional. A Unix timestamp. If it is null or left out, the current time is used.

Return Value

Return ValueDescription
intThe requested part of the date as an integer.
falseReturned with a warning if the format character is not recognized.

PHP Examples

Example 1 — Read One Part at a Time

Each call returns a single integer for the part you ask for.

PHP
<?php
date_default_timezone_set('UTC');

$ts = 1700000000;

echo idate('Y', $ts), PHP_EOL;
echo idate('m', $ts), PHP_EOL;
echo idate('d', $ts), PHP_EOL;
echo idate('H', $ts), PHP_EOL;

How It Works

  1. idate('Y') returns the four-digit year.
  2. idate('m') and idate('d') return the month and day as numbers.
  3. idate('H') returns the hour in 24-hour time.
  4. Each call needs its own format character. You cannot combine them.

Example 2 — Integers vs Strings and Bad Formats

Compare idate() with date(), then see what happens with a format longer than one character.

PHP
<?php
date_default_timezone_set('UTC');

$ts = 1699185600; // 5 November 2023, 12:00 UTC

var_dump(date('d', $ts));
var_dump(idate('d', $ts));

try {
    idate('Ym', $ts);
} catch (ValueError $e) {
    echo 'Error: ', get_class($e), PHP_EOL;
}

How It Works

  1. date('d') returns the string '05', which keeps the leading zero.
  2. idate('d') returns the integer 5, with no zero.
  3. 'Ym' has two characters, so PHP 8 throws a ValueError.
  4. The try/catch block lets the script print a message instead of stopping.

Example 3 — Leap Year Check

Use the L format to test whether a year is a leap year.

PHP
<?php
date_default_timezone_set('UTC');

foreach ([2023, 2024, 2100] as $year) {
    $ts = mktime(0, 0, 0, 1, 1, $year);
    echo $year, ': ', idate('L', $ts) ? 'leap year' : 'not a leap year', PHP_EOL;
}

How It Works

  1. mktime() builds a timestamp for 1 January of each year.
  2. idate('L') returns 1 for a leap year and 0 for a normal year.
  3. The ternary operator turns that number into a readable message.
  4. 2100 is not a leap year because years divisible by 100 must also be divisible by 400.

Frequently Asked Questions

It returns one part of a date or time, such as the year, month or hour, as an integer.
date() returns a formatted string and can use many letters at once. idate() returns one integer for a single letter, with no leading zeros.
The format must be exactly one character. In PHP 8, passing a longer string, like 'Ym', throws a ValueError.
Yes. It uses the default time zone, like date(). Use date_default_timezone_set('UTC') for stable results.
Use idate('t', $timestamp). For a timestamp in February 2024 it returns 29.

Next: localtime()

localtime() returns the parts of a date and time as an array, using the names from the C '.

localtime() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful