Home PHP Date/Time idate() PHP idate() Function 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.
$year = idate('Y', 1700000000); Call What 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 idate(string $format, ?int $timestamp = null): int|false Parameters Parameter Description format Required. One character that says which part to return. A longer string throws a ValueError. timestamp Optional. A Unix timestamp. If it is null or left out, the current time is used.
Return Value Return Value Description int The requested part of the date as an integer. false Returned with a warning if the format character is not recognized.
PHP Examples 1. Read One Part at a Time 2. Integers vs Strings and Bad Formats 3. Leap Year Check Example 1 — Read One Part at a Time Each call returns a single integer for the part you ask for.
<?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 idate('Y') returns the four-digit year. idate('m') and idate('d') return the month and day as numbers. idate('H') returns the hour in 24-hour time. 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
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;
} string(2) "05"
int(5)
Error: ValueError How It Works date('d') returns the string '05', which keeps the leading zero. idate('d') returns the integer 5, with no zero. 'Ym' has two characters, so PHP 8 throws a ValueError. The try/catch block lets the script print a message instead of stopping. Frequently Asked Questions What does idate() do in PHP? It returns one part of a date or time, such as the year, month or hour, as an integer.
What is the difference between idate() and date()? date() returns a formatted string and can use many letters at once. idate() returns one integer for a single letter, with no leading zeros.
Why does idate() throw a ValueError? The format must be exactly one character. In PHP 8, passing a longer string, like 'Ym', throws a ValueError.
Does idate() use the default time zone? Yes. It uses the default time zone, like date(). Use date_default_timezone_set('UTC') for stable results.
How do I get the number of days in a month with idate()? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion