Home > Backend Development > PHP Tutorial > A bunch of date and time operations in php

A bunch of date and time operations in php

步履不停
Release: 2023-04-06 20:24:02
Original
4339 people have browsed it

A bunch of date and time operations in php

下载 (1).jpg

Format date and time

date : Format Date and time

  • Scenario

Format the current date and time or a specific date and time ##The output is a string in a specific format, often used for humanized display of information.

  • Description

Return to Date and time string generated after formatting a certain timestamp. If no timestamp is given, the local current time will be used by default.

  • Remarks

FormatDescriptionReturn value example##y219##M##m01 to 12The day of the week, the text indicates, The day of the month, hour, hour, Whether it is daylight saving time, otherwise it is minutes with leading zeros Number The English suffix after the number of days in a month, 2st,nd,rdj Number of seconds, with leading zeros ##
  • Common formats

// 形如 2019-05-31 12:00:00echo date("Y-m-d H:i:s");
// 形如 2019/05/31 12:00:00echo date("Y/m/d H:i:s");
// 形如 2019年05月31日 12时00分00秒echo date("Y年m月d日 H时i分s秒");
Copy after login
  • Examples

<?php
// 设置当前时区为上海时区
date_default_timezone_set("Asia/Shanghai");

// 获取当前时区 : Asia/Shanghai
echo "当前时区 : ".date_default_timezone_get()."<br/>";

// `Y年m月d日 H时i分s秒` 格式化当前时间 : 2019年05月30日 22时32分46秒
echo "当前时间 : ".date("Y年m月d日 H时i分s秒")."<br/>";

// `Y-m-d H:i:s` 格式化当前时间 : 2019-05-30 22:32:46
echo "当前时间 : ".date("Y-m-d H:i:s")."<br/>";

// `w` 星期中的第几天,数字表示: 0(表示星期天)到 6(表示星期六)
switch (date("w")) {    
    case &#39;0&#39;:
        $dayStr = "日";        
        break;    
    case &#39;1&#39;:
    $dayStr = "一";        
    break;    
  case &#39;2&#39;:
    $dayStr = "二";        
    break;    
  case &#39;3&#39;:
    $dayStr = "三";      
    break;   
  case &#39;4&#39;:
    $dayStr = "四";        
    break;    
  case &#39;5&#39;:
    $dayStr = "五";        
    break;    
  case &#39;6&#39;:
    $dayStr = "六";        
    break;    
  default:
    $dayStr = "未知";        
    break;
} 
// 2019年05月30日 星期四
echo "当前时间 : ".date("Y年m月d日")." 星期".$dayStr."<br/>";
echo "<hr/>";

// `z` 年份中的第几天 : 今天是全年的第149天
echo "今天是全年的第".date("z")."天<br/>";

// `W` ISO-8601 格式年份中的第几周,每周从星期一开始 : 本周是全年的第22周
echo "本周是全年的第".date("W")."周<br/>";

// `t` 指定的月份有几天 : 本月共有31天
echo "本月共有".date("t")."天<br/>";
?>
Copy after login

Date conversion timestamp

time: Returns the current Unix timestamp

  • Scenario

Get the timestamp of the current date and time or a specific date and time, often used for conversion between dates and times.

  • Description

Returns the current time since the Unix epoch (January 1, 1970 00:00:00 GMT) The number of seconds.

  • Example

<?php
// 设置当前时区为上海时区
date_default_timezone_set("Asia/Shanghai");

// 获取当前时区
echo "当前时区 : ".date_default_timezone_get()."<br/>";

// 一周前的日期时间: 7 days; 24 hours; 60 mins; 60 
secs$preWeek = time() - (7 * 24 * 60 * 60);
echo "现在是".date("Y-m-d H:i:s").",上周是".date("Y-m-d H:i:s",$preWeek)."<br/>";

// 一周后的日期时间: 7 days; 24 hours; 60 mins; 60 secs
$nextWeek = time() + (7 * 24 * 60 * 60);
echo "现在是".date("Y-m-d H:i:s").",下周是".date("Y-m-d H:i:s",$nextWeek)."<br/>";
?>
Copy after login

microtime : Returns the current Unix Timestamp and microseconds

  • Scenario

Get the time of the current date-time or a specific date-time Stamp, often used for point analysis of program running process, and can also be used for conversion between dates and times.

  • Explanation

The current Unix timestamp and microseconds. This function is only available under operating systems that support the `gettimeofday()`` system call.

  • ##Example

  • <?php
    
    // 设置当前时区为上海时区
    date_default_timezone_set("Asia/Shanghai");
    
    // 获取当前时区
    cho "当前时区 : ".date_default_timezone_get()."<br/>";
    
    // 当前日期时间戳
    echo "当前日期时间戳: ".time()." <--> ".microtime()." <--> ".microtime(TRUE)."<br/>";
    
    ?>
    Copy after login
mktime: Get the Unix timestamp of a date

  • ##Scenario

  • Get the timestamp of a given date, parse it in sequence according to the "hour, minute, second, month, day and year" format, and return the timestamp.

  • Description

  • Return the Unix timestamp according to the given parameters.

  • Remarks

Y 4 Complete year with digits 2019
Year represented by digits
Three-letter abbreviation for the month Jan to Dec
The month represented by the number, with leading zeros # #D
3 letters Mon to Sun d
2 digits with leading zeros 01 to 31 H
24 hour format, with leading zeros 00 to 23 h
12 hour format, with leading zeros 01 to 12 I
If so Daylight saving time is 10 ##i
00 to 59 S
characters or th, can be used together with s
00 to 59
FormatHhour00 to 23##i Minutes ##sday01 to 31year Year number, can be two or four digits corresponds to corresponds to
DescriptionParameter example
Number of hours
minute00 to 59
second Number of seconds 00 to 59# #n
month Number of months 01 to 12## j
Number of days##Y
0-692000-2069 ,70-1001970-2000

格式: 时分秒 月日年,支持从右往左依次省略,被省略的值取当前时间的对应值.

  • 示例

<?php
// 设置当前时区为上海时区
date_default_timezone_set("Asia/Shanghai");

// 获取当前时区
echo "当前时区 : ".date_default_timezone_get()."<br/>"; 

// 指定日期时间戳: 时分秒 月日年 : 1559275200 <--> 2019-05-31 12:00:00
echo "2019年05月31日 12:00:00 的时间戳: ".mktime(12,0,0,5,31,2019)." <--> ".date("Y-m-d H:i:s", mktime(12,0,0,5,31,2019))."<br/>"; 

// 距离国庆节还有多少天,单位秒 : 今天是2019-05-31,距离国庆节还剩122天
$nationalDay = mktime(0,0,0,10,1,2019);
$currentDay = time();
$remainingDay = floor(abs($nationalDay - $currentDay)/(24*3600));
echo "今天是".date("Y-m-d").",距离国庆节还剩".$remainingDay."天<br/>";
?>
Copy after login

strtotime : 将任何字符串的日期时间描述解析为 Unix 时间戳

  • 场景

将英文日期解析成时间戳,比直接解析日期方便,采用自然语义而不是编程语言进行转换日期.

  • 说明

本函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数,其值相对于 now 参数给出的时间,如果没有提供此参数则用系统当前时间.

  • 常用格式

// 2019-06-02
echo date("Y-m-d", strtotime("2019-05-31 +2 days"));

// 2019-07-01
echo date("Y-m-d", strtotime("2019-05-31 +1 month"));

// 2019-06-09
echo date("Y-m-d", strtotime("2019-05-31 +1 week 2 days 4 hours 2 seconds"));
Copy after login
  • 示例

<?php
// 设置当前时区为上海时区
date_default_timezone_set("Asia/Shanghai");

// 获取当前时区
echo "当前时区 : ".date_default_timezone_get()."<br/>";

// 当前日期时间戳
echo "当前日期时间戳: ".time()." <--> ".strtotime("now")." <--> ".date("Y-m-d H:i:s", strtotime("now"))."<br/>";

// 一周后的日期时间: 7 days; 24 hours; 60 mins; 60 secs
$nextWeek = time() + (7 * 24 * 60 * 60);
echo "现在是".date("Y-m-d H:i:s").",下周是".date("Y-m-d H:i:s",$nextWeek)." <--> ".date("Y-m-d H:i:s",strtotime("+1 week"))."<br/>";

echo "现在是".date("Y-m-d H:i:s").",1周2天4小时2秒是".date("Y-m-d H:i:s",strtotime("+1 week 2 days 4 hours 2 seconds"))."<br/>";

echo "现在是".date("Y-m-d H:i:s").",下周三是".date("Y-m-d H:i:s",strtotime("next Thursday"))."<br/>";
?>
Copy after login


日期时间函总结

日期时间函数库是 php 内置的函数库,默认情况下已启用,值得注意的是,日期时间和时区有关,建议首先设置下时区.

纵观日期时间的操作方法,总的来说,可以大致分为两类,一类是给计算机用的,另一类是给人看的.

  • 给人看的

date_default_timezone_set("Asia/Shanghai") : 设置当前脚本使用的时区date("Y-m-d H:i:s") : 格式化日期时间date("Y-m-d", strtotime("2019-05-31 +2 days")) : 格式化英文描述的日期时间

  • 给计算机用的

time() : 当前时间的秒数microtime() : 当前时间的秒数和微秒数strtotime() : 将字符串形式的日期时间转换成时间戳

最后,文档那么齐全,不懂就去多看看,忘记有啥方法全靠 ide 智能提示就好,多用用就会慢慢熟练。

推荐教程:PHP制作阴阳历转换的日历插件

The above is the detailed content of A bunch of date and time operations in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template