We now have a youtube channel. Subscribe!

PHP | gmdate() Function

PHP gmdate() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP gmdate() Function.

The built-in gmdate() function accepts a format string as a parameter, then formats the local GMT/UTC date and time in the specified format.

Syntax

Following below is the syntax to use this function -

gmdate($format, $timestamp)


Parameter Details

Sr.NoParameter & Description
1

format (Mandatory)

This is a format string specifying the format in which you want the output date string to be.

2

timestamp (Optional)

This is an integer value representing the timestamp of the required date


Return Value

This PHP function returns the current date and time in the specified format.

PHP Version

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

Example1

Following example illustrates the usage of the PHP gmdate() function -

<?php
   $date = gmdate("D M d Y");
   print("Date: ".$date);
?>

Output

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

Date: Sun Nov 22 2020

Example2

The following example formats the current date using this PHP function and prints the sunrise and sunset information using of the resultant date -

<?php
   $date = gmdate("H:i:s");
   $sun_info = date_sun_info($date, 20.5937, 78.9629);
   print_r($sun_info);
?>

Output

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

Array
(
    [sunrise] => 4818
    [sunset] => 44087
    [transit] => 24453
    [civil_twilight_begin] => 3381
    [civil_twilight_end] => 45524
    [nautical_twilight_begin] => 1729
    [nautical_twilight_end] => 47176
    [astronomical_twilight_begin] => 98
    [astronomical_twilight_end] => 48807
)

Example3

Now let us invoke the gmdate() function by passing a timestamp -

<?php
   $ts = 1022555568;
   $date = gmdate("D M d Y", $ts);
   print($date);
?>

Output

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

Tue May 28 2002

Example4

Try the following example -

<?php
   date_default_timezone_set('UTC');   
   echo gmdate("l");
   echo "\n";   
   echo gmdate('l dS \of F Y h:i:s A');
   echo "\n";
?>

Output

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

Sunday 
Sunday 22nd of November 2020 12:05:09 AM


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

Do feel free to ask your questions where necessary and we 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.

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.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain