PHP | strftime() Function
Last Updated :
06 Jul, 2021
The strftime() function is an inbuilt function in PHP that formats local time or date according to locale settings i.e. it formats local time or date for location set for it of a place.
Syntax:
strftime( $format, $timestamp )
Parameters: This function accept two parameters as mentioned above and described below:
- $format: This parameter defines the format for the date and time it is a must required parameter.
- $timestamp: The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().
Return Values: It returns a string formatted to $format using the given $timestamp(if mentioned explicitly, otherwise takes default time). Month and weekday names and other language-dependent strings respect the current locale set with setlocale().
Example:
PHP
<?php
// This program prints the current day
setlocale(LC_TIME, "C");
echo strftime("%A");
?>
Output:
Thursday
Format: Following are the values that can be added to $format for a desired output.
FORMAT | DESCRIPTION | EXAMPLE |
---|
%H | Two digit representation of the hour in 24-hour format it's like train timing format. | 00 through 23 |
%k | Hour in 24-hour format, with a space preceding single digits | 0 through 23 |
%I | Two digit representation of the hour in 12-hour format | 01 through 12 |
%l | 12-hour format representation, with a space preceding single digits. | 1,2,3,.,12 |
%M | Two digit representation of the minute | 00 to 59 |
%p | UPPER-CASE 'AM' or 'PM' based on the given time, 11:59 after will AM before will AM. | AM for 00:31, PM for 22:23 |
%P | lower-case 'am' or 'pm' based on the given time, 11:59 after will pm before will am. | am for 00:31, pm for 22:23 |
%r | Same as "%I:%M:%S %p" | 02:22:22 PM for 14:22:22 |
%R | Same as "%H:%M" | 00:44 for 12:44 AM, 17:45for 5:45PM |
%S | It,s two digit representation of seconds. | 00 to 59 |
%T | Same as "%H:%M:%S" | 20:24:37 for 08:24:37 PM |
%X | It represent preferred time representation without the date, based on locale. | 04:44:16 or 16:44:16 |
%z | The time zone offset. Not implemented as described on Windows. See below for more information. | 0500 for US Eastern Time |
%Z | It represent time zone by reducing in 2 or three character. | EST for Eastern Time |
- Time and Date stamps formatting:
FORMAT | DESCRIPTION | EXAMPLE |
---|
%c | Preferred date and time stamp based on locale. | Tue Jan 5 00:55:25 2009 for January 5, 2009 at 12:55:25 AM |
%D | Same as "%m/%d/%y" | 01/05/09 for January 5, 2009 |
%F | Same as "%Y-%m-%d" used in database datestamps. | 2009-01-05 for January 5, 2009 |
%s | Unix Epoch Time timestamp same as the time() function. | 1525376494 for February 27, 2020 04:50:00 PM |
%x | Preferred date representation without the time based on locale. | 01/05/09 for January 5, 2009 |
FORMAT | DESCRIPTION | EXAMPLE |
---|
%a | It reduced the textual representation of the day name. | Sun,Mon, ... |
%A | It's the textual representation of the full day name. | Sunday,Monday,.. |
%d | It represent the day in double digits even day one represent like 01. | 01 to 31. |
%e | It represent the day in single digits but day ten of the month present like 10. | 1 to 31. |
%j | It represent the year in triple digits even day one represent like 001. | 001 to 366. |
%u | ISO-8601 numeric representation of the day of the week. | 1 for Monday to 7 for Sunday. |
%w | It is the numeric representation of the day of the week count start from 0. | 0 for Sunday to 6 for Saturday. |
FORMAT | DESCRIPTION | EXAMPLE |
---|
%U | Number of week in the given year, starting from first Sunday as the first week | 13 there are 13th full week of the year, |
%V | ISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week | 01 to 53 where 53 accounts for an overlapping week. |
%W | It is numeric representation of week of the year, starting from first Monday as the first week like %U. | 46, 46th week of the year beginning with a Monday. |
FORMAT | DESCRIPTION | EXAMPLE |
---|
%b | It reduced the textual representation of the month name based on the locale. | Jan, Feb, ... |
%B | It's the textual representation of the full month name based on the locale. | January, February, .. |
%h | It reduced the textual representation of the month name based on the locale (an alias of %b). | Jan, Feb, .. |
%m | It represents the day in double digits even month one represents like 01. | 01 for January, 02 for February and so on. |
FORMAT | DESCRIPTION | EXAMPLE |
---|
%C | It represent the century in double digits (year divided by 100, truncated to an integer). | 19 for the 20th Century |
%g | It represent the year in double digits by ISO-8601:1988 standards (see %V) | 09 for the week of January 6, 2009 |
%G | It is the full four-digit version of %g. | 2008 for the week of January 3, 2009 |
%y | It is the two digit representation of the year. | 09 for 2009, 79 for 1979 |
%Y | Four digit representation for the year | 2038 |
- Miscellaneous formatting:
FORMAT | DESCRIPTION |
---|
%n | It is a newline character ("\n") |
%t | It is a tab character ("\t") |
%% | It is a literal percentage character ("%") |
Below examples illustrate the application of strftime() in php:
Example 1: A simple program to display the date and time provided to it.
PHP
<?php
// Displays the date
echo strftime("%d, %B, %Y", strtotime("01/03/2004"));
// Displays the time
echo strftime(" %I:%M %p", strtotime("21:34"));
?>
Output:
03, January, 2004 09:34 PM
Example 2: This example display the time at a specific region (an additional function, setlocale() is employed for this. For setlocale() to work, locales should be supported by your server.)
PHP
<?php
// Setting locale to german
setlocale(LC_ALL, "de");
echo strftime("The current german time is %r");
// Setting locale to english
setlocale(LC_ALL, "en");
echo strftime(" and the current english time is %r");
?>
Output:
The current german time is 22:14:20 and the current english time is 10:14:20 PM
Reference: https://www.php.net/manual/en/function.strftime.php
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read