Table of Contents
日期
礼拜
月份
年份
时间
Home php教程 php手册 PHP教程:时间格式化那些事

PHP教程:时间格式化那些事

Jun 06, 2016 pm 08:09 PM
php Tutorial time format

说到,时间的格式化,很多人可能都会说,其实那很简单的。诚然的,时间的格式化确实很简单,不过呢,可能有一些比较偏的用法你可未必知道哦。时间格式化呢,就是通过 strftime 函数来完成的,这个函数的使用格式是 string strftime(string $format[,int $tim

说到,时间的格式化,很多人可能都会说,其实那很简单的。诚然的,时间的格式化确实很简单,不过呢,可能有一些比较偏的用法你可未必知道哦。时间格式化呢,就是通过strftime函数来完成的,这个函数的使用格式是string strftime(string $format[,int $timestamp=time()])。这意味着strftime函数返回的将是一个string类型的值,有两个参数,第一个参数是格式化的规则,第二个是时间戳。如果省去第二个参数,那么,将自动调用系统当前时间戳。下面,我们来看看格式化的规则:

日期

%a:当前区域星期几的简写(Sun 到 Sat)
%A:当前区域星期几的全称(Sunday 到 Saturday)
%d:月份中的第几天,十进制数字(范围从 01 到 31)
%e:月份中的第几天,十进制数字,一位的数字前会加上一个空格(范围从 ' 1' 到 '31')
%j:年份中的第几天,带前导零的三位十进制数(范围从 001 到 366)
%u:符合 ISO-8601 星期几的十进制数表达 [1,7],1 表示星期一(1【星期一】 到 7 【星期日】)
%w:星期中的第几天,星期天为 0(0 【星期天】 到 6 【星期六】)

礼拜

%U:本年的第几周,从第一周的第一个星期天作为第一天开始
%V:本年第几周的 ISO-8601:1988 格式,范围从 01 到 53,第 1 周是本年第一个至少还有 4 天的星期,星期一作为每周的第一天。(用 %G 或者 %g 作为指定时间戳相应周数的年份组成,可选值01 到 53 。)
%W:本年的第几周数,从第一周的第一个星期一作为第一天开始

月份

%b:当前区域月份的简写【Jan 到 Dec】
%B:当前区域月份的全称【January 到 December】
%h:当前区域月份的简写【(%b 的别名)Jan 到 Dec】
%m:两位数的月份【01 (是一月份) 到 12 (是十二月份)】

年份

%C:两位数显示世纪(年份除以 100,截成整数)【19 是 20 世纪】
%g:2位数的年份,符合 ISO-8601:1988 星期数(参见 %V)。【和 %V 的格式和值一样,只除了如果 ISO 星期数属于前一年或者后一年,则使用那一年。比如:2009年1月6日那一周是 09。】
%G:%g 的完整四位数版本【比如:2009年1月3日那一周是 2008】
%y:两位数显示年份 【比如:09 是 2009,79 是 1979】
%Y:四位数显示年份 【比如:2038】

时间

%H:以 24 小时格式显示两位小时【00 到 23】
%I:以 12 小时格式显示两位小时数【01 到 12】
%l('L' 的小写):以 12 小时格式显示小时数,单个数字前含空格【1 到 12】
%M:两位的分钟数【00 到 59】
%p:指定时间的大写 “AM” 或 “PM”【比如: 00:31 是 AM ,22:23 是PM】
%P:指定时间的小写 “am” 或 “pm”【比如:00:31 是 am ,22:23 是pm】
%r:和 "%I:%M:%S %p" 一样 【比如: 21:34:17 是 09:34:17 PM】
%R:和 "%H:%M" 一样【比如: 12:35 AM 是 00:35,4:44 PM 是 16:44】
%S:两位数字表示秒【00 到 59】
%T:和 "%H:%M:%S" 一样【比如: 09:34:17 PM 是 21:34:17】
%X:当前区域首选的时间表示法,不包括日期【例如: 03:59:16 或 15:59:16】
%z:从 UTC 的时区偏移 或 简写(由操作系统决定)【比如: 东部时间是 -0500 或 EST】
%Z:%z 没有给出的 UTC 的时区偏移 或 简写(由操作系统决定)【比如: -0500 或 EST 是东部时间】

关于时区的设置,时区设置可以利用%z或者%Z来决定,当然,我们还可以利用date_default_timezone_set函数在格式化之前设置时区,例如:通过date_default_timezone_set('Europe/London');把时区设置成欧洲伦敦所在时区。值得大家注意的是,%z或者%Z来决定来决定时区会受到操作系统的影响。而通过date_default_timezone_set函数来设置时区,这个需要PHP5.1.0以上(含5.1.0版本)才支持这个函数。

声明: 本文采用 BY-NC-SA 协议进行授权 | IT路人
转载请注明转自《PHP教程:时间格式化那些事》

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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.

See all articles