Sunday, November 22, 2020

PHP | gmstrftime() Function

PHP gmstrftime() 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 gmstrftime() Function.

The gmstrftime() function accepts a format string as a parameter, and then formats the GMT/UTC time/date according to the locale settings.

Syntax

Following below is the syntax to use this function -

gmstrftime($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 date/time.

2

timestamp(Optional)

This is an integer value representing the Unix time stamp specifying the current time value.


Return Value

This function returns a string value which represents the formatted time. You can change the month and week day names to any other language making use of the built-in PHP setlocale() function.

PHP Version

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

Example1

Following example demonstrates the usage of the PHP gmstrftime() function -

<?php
   $date = gmstrftime("%A %d %B %G");
   $time = gmstrftime("%T");
   print("Date: ".$date ."\n");
   print("Time: ".$time);
?>

Output

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

Date: Sunday 22 November 2020 
Time: 08:32:33

Example2

Let us now invoke this function by passing the time stamp parameter (along with the format) -

<?php
   $timestamp = mktime(7, 36, 45, 06, 25, 2017); 
   $date = gmstrftime("%A %d %B %G %T", $timestamp );
   print("Date: ".$date ."\n");
?>

Output

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

Date: Sunday 25 June 2017 07:36:45

Example3

Following example below prints the day of the week and month of a particular date in the Catalan language -

<?php
   setlocale(LC_TIME, 'ca_ES', 'Catalan_Spain', 'Catalan');
   $date = gmstrftime("%A %d %B %G %T");
   print("Date: ".$date ."\n");
?>

Output

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

Date: dimecres 13 maig 2020 17:28:16 

Example4

Try the following example -

<?php
   setlocale(LC_TIME, 'en_US');   
   echo strftime("%b %d %Y %H:%M:%S", mktime(9, 45, 30, 12, 31, 2015)) . "\n";
   echo gmstrftime("%b %d %Y %H:%M:%S", mktime(9, 45, 30, 12, 31, 2015)) . "\n"; 
?>

Output

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

Dec 31 2015 20:00:00
Dec 31 2015 20:00:00


Following below are the various characters to format the date and time using strftime &minus -

  • %a - Abbreviated weekday name.
  • %A - Full weekday name.
  • %b - An abbreviated month name.
  • %B - Full month name.
  • %c - Preferred date and time representation.
  • %C - Century number (year divided by 100, ranges from 00 to 99).
  • %d - Day of the month (01 to 31).
  • %D - Same as %m/%d/%y.
  • %e - Day of the month (1 to 31).
  • %g - Like %G, but without the century.
  • %G - Its is a 4-digit year that correspond to the ISO week number (see %V).
  • %h - Same as %b.
  • %H - Hours, using a 24-hour clock (00 to 23).
  • %I - Hours, using a 12-hour clock (01 to 12).
  • %j - Day of the year (001 to 366).
  • %m - Month (01 to 12).
  • %M - Minute.
  • %n - Newline character.
  • %p - Either am or pm based on the given time value.
  • %r - Time in a.m. and p.m. notation.
  • %R - This is time in 24-hour notation.
  • %S - Second.
  • %t - Tab character.
  • %T - Current time, equal to %H/%M/%S.
  • %u - Weekday as a number (1 to 7), Monday=1. Note: In Sun Solaris, Sunday=1.
  • %U - It's the week number of the current year, beginning with the first Sunday as the first day of the first week.
  • %V - It is the ISO 8601 week number of the current year (01 to 53), where week 1 is the first week with at least 4 days in the current year, and with Monday as the first day of the week.
  • %W - Its the week number of the current year, beginning with first Monday as the first day of the first week.
  • %w - It's the day of the week as a decimal, Sunday=0.
  • %x - It is the preferred date representation without time.
  • %X - It is the preferred time representation without date.
  • %y - This is the year without a century (00 to 99).
  • %Y - It is the year including the century.
  • %Z or %z - The time zone or name or abbreviation.
  • %% - A literal % character.


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 idate() 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.