Home Backend Development PHP Tutorial Summary of date and time operation skills in PHP introductory tutorial (formatting, verification, acquisition, conversion, calculation, etc.)

Summary of date and time operation skills in PHP introductory tutorial (formatting, verification, acquisition, conversion, calculation, etc.)

Dec 22, 2016 pm 01:53 PM

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 &#39;这个日期是合法有效的&#39;;
 }else{
  echo &#39;这个日期是非法的。&#39;;
 }
?>
Copy after login

Demo2.php

<?php
 //date -- 格式化一个本地时间/日期
 //date(), 彻底研究一下
 //date() 可以存放两个参数,第一参数是日期和时间的格式化,[第二参数是时间戳]
 //Y 表示四位数的年份, y表示二位数的年份
 //M 表示英文的月份缩写,m 表示阿拉伯数字的月份
 //D 表示英文下的星期几缩写,d 表示阿拉伯数字的日
 //第一个参数的格式化可以放一些无关紧要的字符串
 //只要无关紧要的字符串不再 format 的目录里,就不会被识别
 //echo date(&#39;现在的日期是:Y-m-d&#39;); //现在的日期是:2015-04-20
 //时分秒 = H 表示24小时制的小时,
 //明明是 19 ,为什么显示 11 点呢,东八区,差 8 个小时
 //现在没有经过任何设置,所以时间在默认时区上
 //echo date(&#39;现在的日期是:Y-m-d H:i:s&#39;);
 //重点是年月日,时分秒
 echo date(&#39;r&#39;);
 echo date(&#39;现在的日期是:Y-m-d H:i:sa&#39;);
?>
Copy after login

Demo3.php

<?php
 //取得当前的时间,返回一个数组
 //"sec" - 自 Unix 纪元起的秒数
 //"usec" - 微秒数
 //"minuteswest" - 格林威治向西的分钟数
 //"dsttime" - 夏令时修正的类型
 //print_r(gettimeofday());
 //第一数组的元素就是时间戳
 //gettimeofday() 就是取得的当前时间的时间戳
 //$a = gettimeofday();
 //sec 取得当前时间的时间戳
 //转换成人可以看得懂的时间
 //第二个参数,对于本例来讲,放与不放,是一样的。
 //echo date(&#39;Y-m-d H:i:s&#39;,$a[&#39;sec&#39;]);
 print_r(gettimeofday(0));
 echo gettimeofday(1);
?>
Copy after login

Demo4.php

<?php
 //将时间戳转换成人可以看的懂的时间
 //date() 函数的第二个参数就是时间戳
 //如果第二个参数省略了,那么就返回当前时间
 //如果第二个参数没有省略,那么就返回那个时间戳的时间
 echo date(&#39;Y-m-d H:i:s&#39;,24554457865);
?>
Copy after login

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[&#39;year&#39;];
 //传递一个时间戳
 print_r(getdate(1029526066));
?>
Copy after login

Demo6.php

<?php
 //直接获取当前时间戳
 //echo time();//1429526328
 //这个 time() 可以调整时间
 //大家可以发现 time() 很有用处,可以过去现在和将来
 echo date(&#39;Y-m-d H:i:s&#39;,time()+60*60*8);
?>
Copy after login

Demo7. php

<?php
 //获取特定指定时间的时间戳
 //这是当前的时间戳
 //echo time();
 //我要取得 2008-08-08 08:08:08
 $beijing2008 = mktime(8,8,8,8,8,2008);
 echo date(&#39;Y-m-d H:i:s&#39;,$beijing2008);
?>
Copy after login

Demo8.php

<?php
 //使用时间戳计算时间差
 $now = time();//当前的时间戳
 $wnow = mktime(0,0,0,8,16,2016);
 //两个时间戳相减可以得到差秒
 echo round(($wnow - $now)/60/60,2).&#39;相差这几个小时&#39;;
?>
Copy after login

Demo9.php

<?php
 //将人可读的时间,字符串形式,转换成时间戳
 $a = strtotime(&#39;2010-7-16 15:15:15&#39;)-strtotime(&#39;2010-7-16 15:14:15&#39;);
 if($a >= 60){
  echo &#39;请这位先生休息一会。&#39;;
 }else{
  echo $a;
 }
?>
Copy after login

Demo10.php

<?php
 //获取当前文件的修改时间戳
 echo date(&#39;Y-m-d H:i:s&#39;,getlastmod());
?>
Copy after login

Demo11.php

<?php
 //配置系统环境变量
 echo date(&#39;Y-m-d H:i:s&#39;);
 echo &#39;<br/>&#39;;
 //我开始设置时区
 putenv(&#39;Tz=Asia/Shanghai&#39;);
 echo date(&#39;Y-m-d H:i:s&#39;);
?>
Copy after login

Demo12.php

<?php
 //putenv(&#39;Tz=Asia/Shanghai&#39;);
 //获取当前时区
 echo date_default_timezone_get();
 echo &#39;<br/>&#39;;
 //开始配置默认时区
 date_default_timezone_set(&#39;Asia/Shanghai&#39;);
 echo date(&#39;Y-m-d H:i:s&#39;) ;
 echo &#39;<br/>&#39;;
 echo date_default_timezone_get();
?>
Copy after login

Demo13.php

<?php
 date_default_timezone_set(&#39;Asia/Shanghai&#39;);
 //"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 )
?>
Copy after login

Demo14.php

<?php
 //返回时间戳和微秒数
 //怎么计算页面运行加载时间
 //页面打开的时候获取一个时间
 //页面结束的时候获取一个时间
 //用结束的时间减去打开的时间,那么就是运行时间
// list($a,$b)=explode(&#39; &#39;,microtime());
// echo $b;
 function fn(){
  list($a,$b)=explode(&#39; &#39;,microtime());
  return $a+$b; //返回出精确的秒数
 }
 //在页面打开的时候,获取一个时间
 $start_time = fn();
 for($i=0;$i<10000000;$i++){
  //
 }
 //页面结束的时候,获取一个时间
 $end_time = fn();
 echo round(($end_time - $start_time),4);
?>
Copy after login

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!


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

Video Face Swap

Video Face Swap

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

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles