Home Backend Development PHP Tutorial PHP Date()函数详细参数

PHP Date()函数详细参数

Jun 20, 2016 pm 12:32 PM

在页面的最前页加上

date_default_timezone_set(PRC);   /*把时间调到北京时间,php5默认为格林威治标准时间*/

date ()

a:   "am"或是"pm" 

A:   "AM"或是"PM" 

d:   几日,两位数字,若不足则补零;从"01"至"31" 

D:    星期几,3个英文字母,如:"Fri" 

F:    月份,英文全名,如:"January" 

h:    12小时制的小时,从"01"至"12" 

H:    24小时制的小时,从"00"至"23" 

g:    12小时制的小时,不补零;从"1"至"12" 

G:    24小时制的小时,不补零;从"0"至"23" 

j:    几日,不足不被零;从"1"至"31" 

l:    星期几,英文全名,如:"Friday" 

m:    月份,两位数字,从"01"至"12" 

n:    月份,两位数字,不补零;从"1"至"12" 

M:    月份,3个英文字母;如:"Jan" 

s:   秒;从"00"至"59" 

S:    字尾加英文序数,两个英文字母,如:"21th" 

t:    指定月份的天数,从"28"至"31" 

U:    总秒数 

w:    数字型的星期几,从"0(星期天)"至"6(星期六)" 

Y:    年,四位数字 

y:    年,两位数字 

z: 一年中的第几天;从"1"至"366"

==================================================================

1,年-月-日

表示年可以用大写的Y和小写y;

表示月可以用大写F、大写M、小写m和小写n(分别表示字符和数字的两种方式);

表示日可以用小写d和小写j,大写S表示日期的后缀。

echo date('Y-m-j');

2007-02-6

echo date('y-n-j');

07-2-6

大写Y表示年四位数字,而小写y表示年的两位数字;

小写m表示月份的数字(带前导),而小写n则表示不带前导的月份数字。

echo date('Y-M-j');

2007-Feb-6

echo date('Y-m-d');

2007-02-06

大写M表示月份的3个缩写字符,而小写m则表示月份的数字(带前导0);

没有大写的J,只有小写j表示月份的日期,无前导o;若需要月份带前导则使用小写d。

echo date('Y-M-j');

2007-Feb-6

echo date('Y-F-jS');

2007-February-6th

大写M表示月份的3个缩写字符,而大写F表示月份的英文全写。(没有小写f)

大写S表示日期的后缀,比如“st”、“nd”、“rd”和“th”,具体看日期数字为何。

2,时:分:秒

默认情况下,PHP解释显示的时间为“格林威治标准时间”,与我们本地的时间相差8个小时。

echo date('g:i:s a');

5:56:57 am

echo date('h:i:s A');

05:56:57 AM

小写g表示12小时制,无前导0,而小写h则表示有前导0的12小时制。

当使用12小时制时需要表明上下午,小写a表示小写的“am”和“pm”,大写A表示大写的“AM”和“PM”。

echo date('G:i:s');

14:02:26

大写G表示24小时制的小时数,但是不带前导的;使用大写的H表示带前导的24小时制小时数

小结:

字母g表示小时不带前导,字母h表示小时带前导;

小写g、h表示12小时制,大写G、H表示24小时制。

3,闰年、星期、天

echo date('L');

今年是否闰年:0

echo date('l');

今天是:Tuesday

echo date('D');

今天是:Tue

大写L表示判断今年是否闰年,布尔值,为真返回1,否则为0;

小写l表示当天是星期几的英文全写(Tuesday);

而使用大写D表示星期几的3个字符缩写(Tue)。

echo date('w');

今天星期:2

echo date('W');

本周是全年中的第 06 周

小写w表示星期几,数字形式表示

大写W表示一年中的星期数

echo date('t');

本月是 28 天

echo date('z');

今天是今年的第 36 天

小写t表示当前月份又多少天

小写z表示今天是本年中第几天

4,其他

echo date('T');

UTC

大写T表示服务器的时间区域设置

echo date('I');

大写I表示判断当前是否为夏令时,为真返回1,否则为0

echo date('U');

大写U表示从1970年1月1日到现在的总秒数,就是Unix时间纪元的UNIX时间戳。

echo date('c');

2007-02-06T14:24:43+00:00

小写c表示ISO8601日期,日期格式为YYYY-MM-DD,用字母T来间隔日期和时间,时间格式为HH:MM:SS,时区使用格林威治标准时间(GMT)的偏差来表示。

echo date('r');

Tue, 06 Feb 2007 14:25:52 +0000

小写r表示RFC822日期。

5、格式化时间

echo $row["t_time"];    将输出 2008-2-29 12:08:00

echo date("Y-m-d",strtotime($row["t_time"]));   将输出 2008-2-29

注,由于$row["t_time"]得到的时间已经是一个字符串,所以需要用strtotime( 字串转时间戳)转一下,否则输出1970-01-01的错误


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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles