PHP | idate() Function

The idate() function is an inbuilt function in PHP which is used to format a local time/date as an integer. The $format and $timestamp are sent as parameters to the idate() function and it returns an integer formatted according to the specified format using the given timestamp. Unlike the function date(), idate() accepts just one char in the format parameter.

Syntax:

int idate( $format, $timestamp )

Parameters: This function accepts two parameters as mentioned above and described below:



  • $format: It is a mandatory parameter which specifies the format of the result. The format parameter can have the following values:
    • B – Swatch Beat/Internet Time
    • d – Day of the month
    • h – Hour (12 hour format)
    • H – Hour (24 hour format)
    • i – Minutes
    • I – returns 1 if DST (daylight saving time) is activated, 0 otherwise
    • L – returns 1 for leap year, 0 otherwise
    • m – Month number
    • s – Seconds
    • t – Days in current month
    • U – Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
    • w – Day of the week (Sunday=0)
    • W – ISO-8601 week number of year (week starts on Monday)
    • y – Year (1 or 2 digits)
    • Y – Year (4 digits)
    • z – Day of the year
    • Z – Timezone offset in seconds
  • $timestamp: It is an optional parameter which specifies a Unix timestamp that represents the date/time to be formatted.

Return Value: It returns an integer value according to the specified format using the given timestamp.

Exceptions:

  • The idate() function throws a E_NOTICE on every call to a date/time if the time zone is not valid.
  • The idate() function throws a E_STRICT or E_WARNING message if using the system settings or the TZ environment variable.

Below programs illustrate the idate() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Formatting local date/time as Year
echo idate("Y") . "<br>";
  
// Formatting local date/time as Hour(24 hr format)
echo idate("H") . "<br>";
  
// Formatting local date/time as Minutes
echo idate("i") . "<br>";
  
// Formatting local date/time as day of the year 
echo idate("z") . "<br>";
?>

chevron_right


Output:

2018
11
22
238

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Parsing English textual datetime description into a Unix timestamp
$timestamp = strtotime('24th August 2018'); 
  
// Formatting local date/time as Year
echo idate('Y', $timestamp);
?>

chevron_right


Output:

2018

Related Articles:

Reference: http://php.net/manual/en/function.idate.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.