The idate() function is a very useful function of PHP to generate integer values from the local date/time value. Different parts of the date and time can be retrieved by using this function, such as the day value of the date, the month value of the date in number, and the year value of the date. Different uses of the idate() function have been shown in this tutorial.
Syntax
The syntax of the idate() function is provided below:
int idate($format, $timestamp )
This function has two arguments. The first argument is mandatory and used to set the format parameter of the output. The second argument is optional and used to set the timestamp value on which the idate() function will be used. If the second argument is not used, then the function will work on the current date. It returns a number based on the first argument value.
Different Format Parameters of idate() Function
The purposes of several commonly used format parameters of idate() have been described in this part of the tutorial:
Format Parameter | Purpose |
d | Prints the day of the date |
m | Prints the month number of the date |
y | Prints the 2-digits year value of the date |
Y | Prints the 4-digits year value of the date |
h | Prints the hour value of the time in 12-hour format |
H | Prints the hour value of the time in 24-hour format |
i | Prints the minute value of the time |
s | Prints the second value of the time |
t | Prints the days of the current month |
U | Prints the timestamp value of the current time |
w | Prints the day of the week number where Sunday = 0 |
W | Prints the ISO-8601 week number of the year |
z | Prints the day of the year |
Uses of idate() Function
Different uses of the idate() function are shown in this part of the tutorial.
Example 1: Use of idate() Function for the Current Date and Time
Create a PHP file with the following example that will print the day, month, year, hour, minute, and second of the current date and time. The six different format parameters have been used in the script to print different parts of the current date and time. The optional argument of the idate() function has not been used in this script.
//Print the day of the current date
echo "The day of the current date is: ". idate("d") . "<br/>";
//Print the month of the current date
echo "The month of the current date in number is: ". idate("m") . "<br/>";
//Print the year of the current date
echo "The year of the current date is: ". idate("Y") . "<br/>";
//Print the hour of the current time
echo "The hour of the current time is: ". idate("h") . "<br/>";
//Print the minute of the current time
echo "The minute of the current time is: ". idate("i") . "<br/>";
//Print the second of the current time
echo "The second of the current time is: ". idate("s") . "<br/>";
?>
The following output will appear after executing the previous script:
Example 2: Use of idate() Function With the DateTime Class
Create a PHP file with the following script that will print the day, month, and year of the current date by using the second argument of the idate() function. The date object has been created by using the DateTime class. The date_timestamp_get() function has been used to read the timestamp value of the date object.
//Create a date object
$dateVal = new DateTime();
//Generate the timestamp value
$timestamp = date_timestamp_get($dateVal);
//Print the date based on the timestamp value
echo "Today is: " . idate("d", $timestamp)."-".idate("m", $timestamp)."-".idate("Y", $timestamp);
?>
The following output will appear after executing the previous script:
Example 3: Use of idate() Function With the date_create() Function
Create a PHP file with the following script that will print the day, month, and year of the particular date by using the second argument of the idate() function. The date object has been created by using the date_create() function. Like the previous example, the date_timestamp_get() function has been used to read the timestamp value of the date object.
//Create a date value
$dateVal = date_create("31 Dec 2022");
//Generate the timestamp value
$timestamp = date_timestamp_get($dateVal);
//Print the date based on the timestamp value
echo "<h3> The date is: " . idate('d', $timestamp)."-".idate("m", $timestamp)."-".idate("y", $timestamp)."</h3>";
?>
The following output will appear after executing the previous script:
Example 4: Use of idate() Function With the strtotime() Function
Create a PHP file with the following script that will print the day, month, and year of the particular date by using the second argument of the idate() function. The timestamp value of the particular date has been generated by using the strtotime() function.
//Generate the timestamp value of the particular date
$timestamp = strtotime('16-Dec-2022');
//Print the day
echo "The day of the date is " . idate('d', $timestamp) . "<br/>";
//Print the month in number
echo "The month of the date is " .idate('m', $timestamp) . "<br/>";
//Print the year
echo "The year of the date is " .idate('Y', $timestamp) . "<br/>";
?>
The following output will appear after executing the previous script:
Conclusion
Four different ways to print the day, month, and year values of the current or the particular date have been shown in this tutorial by using multiple examples. Two built-in functions of PHP have been used here to get the timestamp value of the specific date. The uses of PHP’s idate() function will be cleared after reading this tutorial.