


The examples in this article describe PHP date and time operation skills. Share it with everyone for your reference, the details are as follows:
Demo1.php
<?php //验证时间 //checkdate() 1.月份 2.日 3.年 //checkdate() 判断这个日期是否是合法的日期 //不合法的日期,试一试 if(checkdate(7,16,2010)){ echo '这个日期是合法有效的'; }else{ echo '这个日期是非法的。'; } ?>
Demo2.php
<?php //date -- 格式化一个本地时间/日期 //date(), 彻底研究一下 //date() 可以存放两个参数,第一参数是日期和时间的格式化,[第二参数是时间戳] //Y 表示四位数的年份, y表示二位数的年份 //M 表示英文的月份缩写,m 表示阿拉伯数字的月份 //D 表示英文下的星期几缩写,d 表示阿拉伯数字的日 //第一个参数的格式化可以放一些无关紧要的字符串 //只要无关紧要的字符串不再 format 的目录里,就不会被识别 //echo date('现在的日期是:Y-m-d'); //现在的日期是:2015-04-20 //时分秒 = H 表示24小时制的小时, //明明是 19 ,为什么显示 11 点呢,东八区,差 8 个小时 //现在没有经过任何设置,所以时间在默认时区上 //echo date('现在的日期是:Y-m-d H:i:s'); //重点是年月日,时分秒 echo date('r'); echo date('现在的日期是:Y-m-d H:i:sa'); ?>
Demo3.php
<?php //取得当前的时间,返回一个数组 //"sec" - 自 Unix 纪元起的秒数 //"usec" - 微秒数 //"minuteswest" - 格林威治向西的分钟数 //"dsttime" - 夏令时修正的类型 //print_r(gettimeofday()); //第一数组的元素就是时间戳 //gettimeofday() 就是取得的当前时间的时间戳 //$a = gettimeofday(); //sec 取得当前时间的时间戳 //转换成人可以看得懂的时间 //第二个参数,对于本例来讲,放与不放,是一样的。 //echo date('Y-m-d H:i:s',$a['sec']); print_r(gettimeofday(0)); echo gettimeofday(1); ?>
Demo4.php
<?php //将时间戳转换成人可以看的懂的时间 //date() 函数的第二个参数就是时间戳 //如果第二个参数省略了,那么就返回当前时间 //如果第二个参数没有省略,那么就返回那个时间戳的时间 echo date('Y-m-d H:i:s',24554457865); ?>
Demo5.php
<?php //getdate() 也可以转换时间戳 //print_r(getdate()); //Array ( [seconds] => 26 [minutes] => 34 [hours] => 10 [mday] => 20 [wday] => 1 [mon] => 4 //[year] => 2015 [yday] => 109 [weekday] => Monday [month] => April [0] => 1429526066 ) $t = getdate(); echo $t['year']; //传递一个时间戳 print_r(getdate(1029526066)); ?>
Demo6.php
<?php //直接获取当前时间戳 //echo time();//1429526328 //这个 time() 可以调整时间 //大家可以发现 time() 很有用处,可以过去现在和将来 echo date('Y-m-d H:i:s',time()+60*60*8); ?>
Demo7. php
<?php //获取特定指定时间的时间戳 //这是当前的时间戳 //echo time(); //我要取得 2008-08-08 08:08:08 $beijing2008 = mktime(8,8,8,8,8,2008); echo date('Y-m-d H:i:s',$beijing2008); ?>
Demo8.php
<?php //使用时间戳计算时间差 $now = time();//当前的时间戳 $wnow = mktime(0,0,0,8,16,2016); //两个时间戳相减可以得到差秒 echo round(($wnow - $now)/60/60,2).'相差这几个小时'; ?>
Demo9.php
<?php //将人可读的时间,字符串形式,转换成时间戳 $a = strtotime('2010-7-16 15:15:15')-strtotime('2010-7-16 15:14:15'); if($a >= 60){ echo '请这位先生休息一会。'; }else{ echo $a; } ?>
Demo10.php
<?php //获取当前文件的修改时间戳 echo date('Y-m-d H:i:s',getlastmod()); ?>
Demo11.php
<?php //配置系统环境变量 echo date('Y-m-d H:i:s'); echo '<br/>'; //我开始设置时区 putenv('Tz=Asia/Shanghai'); echo date('Y-m-d H:i:s'); ?>
Demo12.php
<?php //putenv('Tz=Asia/Shanghai'); //获取当前时区 echo date_default_timezone_get(); echo '<br/>'; //开始配置默认时区 date_default_timezone_set('Asia/Shanghai'); echo date('Y-m-d H:i:s') ; echo '<br/>'; echo date_default_timezone_get(); ?>
Demo13.php
<?php date_default_timezone_set('Asia/Shanghai'); //"tm_sec" - 秒数 //"tm_min" - 分钟数 //"tm_hour" - 小时 //"tm_mday" - 月份中的第几日 //"tm_mon" - 年份中的第几个月,从 0 开始表示一月 //"tm_year" - 年份,从 1900 开始 //"tm_wday" - 星期中的第几天 //"tm_yday" - 一年中的第几天 //"tm_isdst" - 夏令时当前是否生效 print_r(localtime(time(),true)); //Array ( [tm_sec] => 37 [tm_min] => 15 [tm_hour] => 19 //[tm_mday] => 20 [tm_mon] => 3 [tm_year] => 115 //[tm_wday] => 1 [tm_yday] => 109 [tm_isdst] => 0 ) ?>
Demo14.php
<?php //返回时间戳和微秒数 //怎么计算页面运行加载时间 //页面打开的时候获取一个时间 //页面结束的时候获取一个时间 //用结束的时间减去打开的时间,那么就是运行时间 // list($a,$b)=explode(' ',microtime()); // echo $b; function fn(){ list($a,$b)=explode(' ',microtime()); return $a+$b; //返回出精确的秒数 } //在页面打开的时候,获取一个时间 $start_time = fn(); for($i=0;$i<10000000;$i++){ // } //页面结束的时候,获取一个时间 $end_time = fn(); echo round(($end_time - $start_time),4); ?>
Hope this article explains It will be helpful to everyone in PHP programming.
For more PHP introductory tutorial date and time operation skills summary (formatting, verification, acquisition, conversion, calculation, etc.) related articles, please pay attention to the PHP Chinese website!

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 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 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 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.

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 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.

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

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.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

Zend Studio 13.0.1
Powerful PHP integrated development environment