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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

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

CakePHP Services CakePHP Services Sep 10, 2024 pm 05:26 PM

This chapter deals with the information about the authentication process available in CakePHP.

See all articles