`

Date and Time in PHP

    博客分类:
  • php
阅读更多
Displaying the current date and time with the date() function

The most used function is one that returns the current date and time, and allows you to format as you wish - the date function. And using it is very simple:
date("formatting_option").

There are a whole range of possible formatting options. You can add your own characters inside the format string too. Here's a list of all the formatting characters.
a 	"am" or "pm"
A 	"AM" or "PM"
B 	Swatch Internet time
d 	day of the month, 2 digits with leading zeros; i.e. "01" to "31"
D 	day of the week, textual, 3 letters; i.e. "Fri"
F 	month, textual, long; i.e. "January"
g 	hour, 12-hour format without leading zeros; i.e. "1" to "12"
G 	hour, 24-hour format without leading zeros; i.e. "0" to "23"
h 	hour, 12-hour format; i.e. "01" to "12"
H 	hour, 24-hour format; i.e. "00" to "23"
i 	minutes; i.e. "00" to "59"
I (capital i) 	"1" if Daylight Savings Time, "0" otherwise.
j 	day of the month without leading zeros; i.e. "1" to "31"
l (lowercase 'L') 	day of the week, textual, long; i.e. "Friday"
L 	boolean for whether it is a leap year; i.e. "0" or "1"
m 	month; i.e. "01" to "12"
M 	month, textual, 3 letters; i.e. "Jan"
n 	month without leading zeros; i.e. "1" to "12"
r 	RFC 822 formatted date; i.e. "Thu, 21 Dec 2000 16:01:07 +0200" (added in PHP 4.0.4)
s 	seconds; i.e. "00" to "59"
S 	English ordinal suffix, textual, 2 characters; i.e. "th", "nd"
t 	number of days in the given month; i.e. "28" to "31"
T 	Timezone setting of this machine; i.e. "MDT"
U 	seconds since the epoch
w 	day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday)
Y 	year, 4 digits; i.e. "1999"
y 	year, 2 digits; i.e. "99"
z 	day of the year; i.e. "0" to "365"
Z 	timezone offset in seconds (i.e. "-43200" to "43200"). The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.


Some examples:

//returns the day (0-31), month (3 letters) and year(4 digits)
print date("d-M-Y");


displays:
09-May-2002

The '-' characters are my formatting strings. I could have used anything else too:

print date("d^^^M^^^Y");


displays:

09^^^May^^^2002


print date("D dS M,Y h:i a");


displays:

Thu 09th May,2002 06:13 pm


Read through the list above - you'll find most possibilities have been thought of, and if all you're trying to do is format the date and/or time, you'll have no problems.

The date function is not only tied to displaying the local datetime on the server. You can also pass it a timestamp (often called a Unix timestamp, since it developed on Unix machines), which is the number of seconds since January 1, 1970 (starting with 1 second after midnight). By default the date function takes the current timestamp, but you can pass any timestamp you want, for example:

print date("l M dS, Y, H:i:s",5678)


displays:

Thursday Jan 01st, 1970, 03:34:38


Be careful though with trying to use one of the reserved format strings as your own format string. If you tried to do something like:

print date("The time is: H.i");


you'd get something like the unexpected result of:

SAST11e 314605e 4638: 23.46


Of the first word, 'The', the 'T' returns the time zone setting of the machine running PHP (SAST in my case), 'h' returns the hour, while at least the 'e' comes though correctly. But from there it's all downhill. In order to get the correct display, you'll need to escape the characters with a backslash, as follows:

print date("\T\he \\t\i\m\e \i\s: H.i");


which displays what we'd originally hoped for:

The time is: 23.54


Just for good measure we had to escape the 't' twice, since '\t' is the special character for a tab. Not the easiest to read at all! Later in this article, we look at the strftime() function, which makes formatting complex strings easier, and also helps with other languages.

There is also a related gmdate() function, which is the same as date(0 except that it returns the Greenwich Mean Time.
分享到:
评论

相关推荐

    php报时区错误,按照网上说的改时间date.timezone根本不管用!怎么解决,看我的.zip

    php报时区错误,按照网上说的改时间date.timezone根本不管用!怎么解决,看我的.zip Warning: phpinfo() [function.phpinfo]: It is not safe to rely on the system's timezone settings. You are *required* to use ...

    PHP.and.MySQL.Web.Development.5th.Edition

    Chapter 19 Managing the Date and Time Chapter 20 Internationalization and Localization Chapter 21 Generating Images Chapter 22 Using Session Control in PHP Chapter 23 Integrating JavaScript and PHP ...

    PHP和MySQL Web Development第五版(2016)原版完整英文版

    19 Managing the Date and Time 20 Internationalization and Localization 21 Generating Images 22 Using Session Control in PHP 23 Integrating JavaScript and PHP 24 Other Useful Features Part V: Building...

    Beginning PHP and MySQL: From Novice to Professional, 5th Edition

    Chapter 12: Date and Time Chapter 13: Forms Chapter 14: Authenticating Your Users Chapter 15: Handling File Uploads Chapter 16: Networking Chapter 17: Session Handlers Chapter 18: Web Services Chapter...

    PHP实战:对象,设计,敏捷(PHP in Action :Objects, Design, Agility )

    8 怎样设计的问题:日期和时间处理 (Design how-to: date and time handling) Part 2: 测试和重构 (Testing and refactoring) 9 测试驱动开发 (Test-driven development) 10 高级测试技术 (Advanced testing ...

    关于php程序报date()警告的处理(date_default_timezone_set)

    在写php程序中有时会出现这样的警告: PHP Warning: date(): It is not safe to rely on the system’... In case you used any of those methods and you are still getting this warning, you most likely misspelled

    如何利用内置PHP灵活性执行外部程序.doc

    echo "Current date and time is: $date"; ``` 3. 函数 exec() 和 passthru() 在PHP的安全模式下,反引号运算符可能不可用,这时可以使用`exec()`函数。`exec()`接受命令作为参数,执行它,并返回命令输出的最后一...

    PHP.7.Real.World.Application.Development

    Chapter 8: Working with Date/Time and International Aspects Chapter 9: Developing Middleware Chapter 10: Looking at Advanced Algorithms Chapter 11: Implementing Software Design Patterns Chapter 12: ...

    HTML5 in Action

    driven mobile app development, powerful features like local storage and WebSockets, superb audio and video APIs, and new layout options using CSS3, SVG, and Canvas, HTML5 has entered its prime time. ...

    php.ini-development

    TTL for user-defined php.ini files (time-to-live) in seconds. Default is 300 seconds (5 minutes) ;user_ini.cache_ttl = 300 ;;;;;;;;;;;;;;;;;;;; ; Language Options ; ;;;;;;;;;;;;;;;;;;;; ; Enable ...

    ECSHOP安装数据库失败date_default_timezone_get()问题

    In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for '8.0/noDST' instead"。这段信息指出了时区设置的...

    Building.Secure.PHP.Apps

    Let's make sure we all get home on time and sleep well at night. Topics This is a quick read, at just over 100 pages. This is a handbook style guide to specific items you can act on. The following ...

    PHP错误提示It is not safe to rely on the system……的解决方法

    在php程序开发中有时会出现类似于这样的警告: PHP Warning: date(): It is not safe to rely on the ... In case you used any of those methods and you are still getting this warning, you most likely missp

    基于PHP的经典建筑网站的设计与实现论文.doc

    The selection of PHP for website development is advantageous due to its low cost, fast speed, and my familiarity with the technology, eliminating the need for additional learning time. Therefore, the ...

    UE(官方下载)

    In this tutorial, we'll cover some of the basics of Unicode-encoded text and Unicode files, and how to view and manipulate it in UltraEdit. Search and delete lines found UEStudio and UltraEdit provide...

    基于ssm+mysql羽毛球馆管理系统源码数据库论文.doc

    出在 the Internet. For instance, if someone ... With its user-friendly interface and real-time information updates, it contributes to a more enjoyable and well-managed sports environment for all users.

    Clean.Data.1785284010

    If you are a data scientist of any level, beginners included, and interested in cleaning up your data, this is the book for you! Experience with Python or PHP is assumed, but no previous knowledge of ...

Global site tag (gtag.js) - Google Analytics