This article mainly introduces the detailed usage of the php strftime function. The strftime() function formats the local time/date according to the regional settings. Generally, it can be used for time formatting. Friends who need it can refer to it
Definition and Usage
strftime() function formats local time/date according to locale settings.
Syntax
strftime(format,timestamp) Parameter Description format is optional. Specifies how results are returned. timestamp Optional. Tips and Notes Tip: Same behavior as gmstrftime(), except that the returned time is local time.
Example
Output the results of strftime() and gmstrftime():
<?php教程
echo(strftime("%b %d %Y %X", mktime(20,0,0,12,31,98)));
echo(gmstrftime("%b %d %Y %X", mktime(20,0,0,12,31,98)));
//输出当前日期、时间和时区
echo(gmstrftime("It is %a on %b %d, %Y, %X time zone: %Z",time()));
?>
Copy after login
strftime The () function can convert date strings in the YYYY-MM-DD HH:MM:SS format into other forms of strings. The syntax of strftime() is strftime(format, date/time, modifier, modifier, ...)
It can format dates and times with the following symbols:
%a
Abbreviation of the day of the week in the current area
Sun to Sat
##%A
The full name of the day of the week in the current area
Sunday to Saturday
%d
The day of the month, decimal number (range from 01 to 31)
01 to 31
##%e
The day of the month, decimal number, a digit will be added before Spaces (ranging from '1' to '31') are not implemented as described on Windows. More information below.
1 to 31
%j
The day of the year as a three-digit decimal number with leading zeros (range 001 to 366)
001 to 366
%u
Conforms to the ISO-8601 decimal representation of the day of the week [1,7], 1 represents Monday
1 (Monday) to 7 (Sunday)
%w
The day of the week, Sunday is 0
0 (Sunday) to 6 (Saturday)
week
---
---
%U
The first week of the year, starting from the first Sunday of the first week as the first day
13 (for the 13th full week of the year)
%V
%V - ISO-8601:1988 format of the week of the year, ranging from 01 to 53, week 1 is the first of the year There are at least 4 days in the week, with Monday as the first day of the week. (Use %G or %g as the year composition of the corresponding week number of the specified timestamp.)
01 to 53 (where 53 accounts for an overlapping week)
% W
The week number of the year, starting from the first Monday of the first week as the first day
46 (for the 46th week of the year beginning with a Monday)
月
---
---
%b
The abbreviation of the current regional month
Jan to Dec
%B
The full name of the current regional month
January to December
%h
Abbreviation of the month in the current region (alias of %b)
Jan to Dec
% m
Two-digit month
01 (is January) to 12 (is December)
year
---
---
%C
Two digits display the century (year divided by 100, truncated to integers)
19 is the 20th century
##%g
2-digit year, conforming to ISO-8601:1988 week number (see %V). Same format and values as %V, except that if the ISO week number belongs to the previous or next year, that year is used.
For example: the week of January 6, 2009 is 09.
%G
The complete four-digit version of %g
For example: the week of January 3, 2009 is 2008.
%y
Two-digit display year
For example: 09 is 2009, 79 is 1979
##%Y
Four-digit display year
For example: 2038
Time
---
-- -
%H
Display two-digit hours in 24-hour format
00 to 23
%I
Display two-digit hours in 12-hour format
01 to 12
%l (lowercase 'L')
Display hours in 12-hour format, single digits with spaces before them
1 to 12
%M
Two-digit minutes Numbers
00 to 59
%p
Specify the uppercase "AM" or "PM" of the time
For example: 00 :31 is AM, 22:23 is PM
%P
The lowercase "am" or "pm" of the specified time
For example: 00 :31 is am, 22:23 is pm
%r
is the same as "%I:%M:%S %p"
For example: 21:34:17 is 09:34:17 PM
%R
is the same as "%H:%M"
For example : 12:35 AM is 00:35, 4:44 PM is 16:44
%S
two digits represent seconds
00 To 59
##%T
is the same as "%H:%M:%S"
For example: 09:34:17 PM is 21 :34:17
%X
The preferred time representation in the current region, excluding the date
For example: 03:59:16 or 15: 59:16
%z
Time zone offset or abbreviation from UTC (determined by the operating system)
For example: Eastern Time is -0500 or EST
%Z
%z No time zone offset or abbreviation for UTC given (determined by the operating system)
For example: - 0500 or EST is Eastern Time
time and date stamp
---
---
%c
The preferred date and time expression in the current region
For example: February 5, 2009 12:45:10 am is Tue Feb 5 00:45:10 2009
%D
Same as "%m/%d/%y"
For example: February 5, 2009 is 02/05/09
%F
Same as "%Y-%m-%d" (commonly used in database datestamps)
For example: February 5, 2009 is 2009-02-05
##%s
Unix epoch timestamp (same as time() function)
For example: September 1979 8:40:00 AM on the 10th is 305815200
%x
. The preferred time representation in the current region, excluding time
. For example: 2009 February 5th is 02/05/09
Other
---
---
%n
Line break ("\n")
---
%t
Tab Characters ("\t")
---
%%
Percent characters on text ("%")
这个参数的最大长度是 1023 个字符。
Warning 尽管 ISO 9889:1999(当前的 C 标准)明确指出一周从星期一开始,但是 Sun Solaris 的一周似乎从星期天开始并作为 1。所以 %u 的结果也许不会和手册里描述得一样。
select strftime('%Y.%m.%d %H:%M:%S','now','localtime'); 结果:2018.06.21 21:41:09 其实更好的用法是这样的,比如统,本月或本年的支出: select strftime('%Y-%m',日期) as 月份,sum(支出) as 月支出 from 流水帐 group by 月份;
Example #1 strftime() locale examples
<?php
setlocale(LC_TIME, "C");
echo strftime("%A");
setlocale(LC_TIME, "fi_FI");
echo strftime(" in Finnish is %A,");
setlocale(LC_TIME, "fr_FR");
echo strftime(" in French %A and");
setlocale(LC_TIME, "de_DE");
echo strftime(" in German %A.\n");
?>
Example #3 Cross platform compatible example using the %e modifier
<?php
// Jan 1: results in: '%e%1%' (%%, e, %%, %e, %%)
$format = '%%e%%%e%%';
// Check for Windows to find and replace the %e
// modifier correctly
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
$format = preg_replace('#(?<!%)((?:%%)*)%e#', '\1%#d', $format);
}
echo strftime($format);
?>
Copy after login
Example #4 Display all known and unknown formats.
<?php
// Describe the formats.
$strftimeFormats = array(
'A' => 'A full textual representation of the day',
'B' => 'Full month name, based on the locale',
'C' => 'Two digit representation of the century (year pided by 100, truncated to an integer)',
'D' => 'Same as "%m/%d/%y"',
'E' => '',
'F' => 'Same as "%Y-%m-%d"',
'G' => 'The full four-digit version of %g',
'H' => 'Two digit representation of the hour in 24-hour format',
'I' => 'Two digit representation of the hour in 12-hour format',
'J' => '',
'K' => '',
'L' => '',
'M' => 'Two digit representation of the minute',
'N' => '',
'O' => '',
'P' => 'lower-case "am" or "pm" based on the given time',
'Q' => '',
'R' => 'Same as "%H:%M"',
'S' => 'Two digit representation of the second',
'T' => 'Same as "%H:%M:%S"',
'U' => 'Week number of the given year, starting with the first Sunday as the first week',
'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',
'W' => 'A numeric representation of the week of the year, starting with the first Monday as the first week',
'X' => 'Preferred time representation based on locale, without the date',
'Y' => 'Four digit representation for the year',
'Z' => 'The time zone offset/abbreviation option NOT given by %z (depends on operating system)',
'a' => 'An abbreviated textual representation of the day',
'b' => 'Abbreviated month name, based on the locale',
'c' => 'Preferred date and time stamp based on local',
'd' => 'Two-digit day of the month (with leading zeros)',
'e' => 'Day of the month, with a space preceding single digits',
'f' => '',
'g' => 'Two digit representation of the year going by ISO-8601:1988 standards (see %V)',
'h' => 'Abbreviated month name, based on the locale (an alias of %b)',
'i' => '',
'j' => 'Day of the year, 3 digits with leading zeros',
'k' => 'Hour in 24-hour format, with a space preceding single digits',
'l' => 'Hour in 12-hour format, with a space preceding single digits',
'm' => 'Two digit representation of the month',
'n' => 'A newline character ("\n")',
'o' => '',
'p' => 'UPPER-CASE "AM" or "PM" based on the given time',
'q' => '',
'r' => 'Same as "%I:%M:%S %p"',
's' => 'Unix Epoch Time timestamp',
't' => 'A Tab character ("\t")',
'u' => 'ISO-8601 numeric representation of the day of the week',
'v' => '',
'w' => 'Numeric representation of the day of the week',
'x' => 'Preferred date representation based on locale, without the time',
'y' => 'Two digit representation of the year',
'z' => 'Either the time zone offset from UTC or the abbreviation (depends on operating system)',
'%' => 'A literal percentage character ("%")',
);
// Results.
$strftimeValues = array();
// Evaluate the formats whilst suppressing any errors.
foreach($strftimeFormats as $format => $description){
if (False !== ($value = @strftime("%{$format}"))){
$strftimeValues[$format] = $value;
}
}
// Find the longest value.
$maxValueLength = 2 + max(array_map('strlen', $strftimeValues));
// Report known formats.
foreach($strftimeValues as $format => $value){
echo "Known format : '{$format}' = ", str_pad("'{$value}'", $maxValueLength), " ( {$strftimeFormats[$format]} )\n";
}
// Report unknown formats.
foreach(array_diff_key($strftimeFormats, $strftimeValues) as $format => $description){
echo "Unknown format : '{$format}' ", str_pad(' ', $maxValueLength), ($description ? " ( {$description} )" : ''), "\n";
}
?>
The above is the detailed content of How to use strftime function in php. For more information, please follow other related articles on the PHP Chinese website!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals.
This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati
Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems.
With a large collection of extensions for many programming languages, VS Code can be c
JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,
A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase.
What is a vowel?
Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase:
a, e, i, o, u
Example 1
Input: String = "Tutorialspoint"
Output: 6
explain
The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total
Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.