Sunday, November 22, 2020

PHP | idate() Function

PHP idate() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP idate() Function.

PHP idate() function accepts a format string as a parameter, then formats the local date and time in the specified format and returns it.

Syntax

Following below is the syntax to use this function -

idate($format, [$timestamp])


Parameter Details

Sr.NoParameter & Description
1

format(Mandatory)

This is a string value representing the format in which you need to format the local date/time.

2

timestamp(Optional)

This is a an integer representing the timestamp which refers to the current local time.


Return Value

This built-in PHP function returns an integer value which represents the formatted date and time.

PHP Version

This function was first introduced as part of core PHP version 5.0 and, it works with all of the later versions.

Example1

Following example illustrates the usage of idate() function -

<?php
   $format = "U";
   $res = idate($format);
   print("Timestamp: ".$res);
?>

Output

When the above code is executed, it will produce the following result -

Timestamp: 1606054288

Example2

The following example invokes the idate() function using timestamp parameter -

<?php
   $date = date_create();
   $timestamp = date_timestamp_get($date);
   $format = "U";
   $res = idate($format, $timestamp);
   print("Timestamp: ".$res);
?>

Output

When the above code is executed, it will produce the following result -

Timestamp: 1606054347

Example3

Now let us see the various format characters for the idate() function and their results -

<?php
   print("B :".idate("B"));
   print("\n");
   print("d :".idate("d"));
   print("\n");
   print("h :".idate("h"));
   print("\n");
   print("H: ".idate("H"));
   print("\n");
   print("i :".idate("i"));
   print("\n"); 
   print("I :".idate("I"));
   print("\n");
   print("L :".idate("L"));
   print("\n");
   print("m :".idate("m"));
   print("\n");
   print( "s :".idate("s"));
   print("\n");
   print("t :".idate("t"));
   print("\n");
   print("U :".idate("U"));
   print("\n");
   print("w :".idate("w"));
   print("\n");
   print("w:".idate("W"));
   print("\n");
   print("y :".idate("y"));
   print("\n");
   print("Y :".idate("Y"));
   print("\n");
   print("z :".idate("z"));
   print("\n");
   print("Z :".idate("Z"));
   print("\n");
?>

Output

When the above code is executed, it will produce the following result -

B :634 
d :22 
h :2 
H: 14 
i :13 
I :0 
L :1 
m :11 
s :22 
t :30 
U :1606054402 
w :0 
w:47 
y :20 
Y :2020 
z :326 
Z :0

Example4

Try the following example -

<?php
   $timestamp = strtotime('1st January 2014');   
   echo idate('y', $timestamp);
   echo"\n";
   echo idate('t', $timestamp);
?>

Output

When the above code is executed, it will produce the following result -

14
31


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP localtime() Function.

Do feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.