• PHP Video Tutorials

PHP gmdate() Function



Definition and Usage

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

Syntax

gmdate($format, $timestamp)

Parameters

Sr.No Parameter & 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 Values

PHP gmdate() function returns the current local time/date in the specified format.

PHP Version

This function was first introduced in PHP Version 4 and, works with all the later versions.

Example

Try out following demonstrates the usage of the gmdate() function −

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

This will produce following result −

Date: Fri May 08 2020

Example

Following example formats the current date using this function and prints the sunrise/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);
?>

This will produce 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
)

Example

Now lets invoke the gmdate() function by passing a timestamp −

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

This will produce following result −

Tue May 28 2002

Example

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

This produces the following result −

Wednesday
Wednesday 13th of May 2020 05:57:30 PM
php_function_reference.htm
Advertisements