search
HomeBackend DevelopmentPHP TutorialPHP时间和日期函数详解_PHP

PHP中所有函数都是UNIX纪元的,即从1970年1月1日开始的。

日期是从这个时候开始的秒数。

当一个函数调用从这时候计的秒数时,就把它当作(timestamp)时间戳。

本地时间函数

1. string date(string format,inieger timestamp)

该函数返回一个表示时间的字符串,是由string format 控制的。
如:

<&#63;
print(date("Y年 m月d日");//输出当前,年月日.
print(date("Y年 m月d日",60*60*24*365*10);//输出1980年1月1日.
&#63;>

也许你会问,怎麽没有timestamp呢?若timestamp为空时,或不写时,表示使用当前时间一刻timestamp.
表示年份的控制符: Y---四位的年份 y---两位的年份
表示月份的控制符: m---从1-12的月份 F---英文月份名 M---简写的月份名
表示日号的控制符: d---前面有0的月份中日期 j--前面没有0的日号
表示星期的控制符: l--英文星期 D--简写的星期
表示小时的控制符: h--从1到12小时 H---从0到23的小时
表示上下午的控制符 a ---am或pm A---AM或PM
表示分钟的控制符: i---取值00-59
表示一年中第多少天: z--一年中的第多少天

2. array getdate(integer timestamp)

该函数返回一个矩阵.
如:

<&#63;
$current_date=getdate();
print($current_date("hours"));
print($current_date("minutes");
print($current_date("seconds");
&#63;>

说明:
元素 描述
hours 24小时格式的小时
mday 月份中日期
minutes 分钟
mon 数字形式的月份
month 月份全称
seconds 秒数
wday 从0到6的数字形式的星期几
weekday 星期几的名称
year 年份
0 时间戳即从1970年1月1日到现在的秒数
yday 一年中数字形式的日期

3. boolean checkdate(integer month,integer day,integer year)
该函数检查日期是否合法.如:

<&#63;
if(checkdate(2,29,1980))
print("日期合法!n");
&#63;>

4. integer time()

该函数获得当前时间戳.如:

<&#63;
print(time());//输出一大串整数
&#63;>

5. integer mktime(integer hour,integer minutes,integer seconds,integer month, integer day,integer year)

该函数返回给出日期的时间戳,即从1970年1月1日到现在的秒数.
如某参数超出范围,该函数也可以解释它,如13月即为第二年的一月.
如:

<&#63;
$currenthour=date("H");
print("50个小时后为:");
print(date("h:i A l F dS,Y",mktime($currenthour+50)));
print("<br>n");
&#63;>

6. string microtime()

该函数返回一个字符串,由当前时间的毫秒数+空格+从1970年开始的秒数

<&#63;
print("start:microtime()<br>n");
for($index=0;$index<1000;$index++)
print("good!");
print("stop:microtime()<br>n");
&#63;>

还有,各林威治标准时间函数

以上所述就是本文的全部内容了,希望大家能够喜欢。

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool