Date Time Format
DATE : Date Time Format
- Scenes
The current date and time or a specific date and time format output string of a specific format, the information used to display user-friendly.
- Explanation
Returns to the time after the date given timestamp formatting the generated string If no local current time stamp is used by default.
- Remark
format | Explanation | Example Return value |
---|---|---|
Y |
4 Year-digit full representation |
2019 |
y |
2 Year-digit representation |
19 |
M |
Three-letter abbreviations of the month | Jan 到 Dec |
m |
The numbers indicate the month, with leading zeros | 01 到 12 |
D |
The first few days of the week, textual representation 3 letters |
Mon 到 Sun |
d |
The first day of the month, with leading zeros 2 digit |
01 到 31 |
H |
Hour, 24 hour format, with leading zeros |
00 到 23 |
h |
Hour, 12 hour format, with leading zeros |
01 到 12 |
I |
Whether daylight saving time | If daylight saving time is 1 otherwise0 |
i |
There are number of minutes leading zeros | 00 到 59 |
S |
Behind the number of days per month English suffix 2 characters |
st,nd,rd Alternatively th , and can be j used together |
s |
The number of seconds, with leading zeros | 00 到 59 |
- Common formats
// 形如 2019-05-31 12:00:00
echo date("Y-m-d H:i:s");
// 形如 2019/05/31 12:00:00
echo date("Y/m/d H:i:s");
// 形如 2019年05月31日 12时00分00秒
echo date("Y年m月d日 H时i分s秒");
- 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 '0':
$dayStr = "日";
break;
case '1':
$dayStr = "一";
break;
case '2':
$dayStr = "二";
break;
case '3':
$dayStr = "三";
break;
case '4':
$dayStr = "四";
break;
case '5':
$dayStr = "五";
break;
case '6':
$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/>";
?>
Date of Conversion timestamp
Time : returns the current Unix timestamp
- Scenes
Get current date and time or a specific date and time stamp, used to convert between the date and time.
- Explanation
Return since Unix
epoch (GMT 1970年1月1日 00:00:00
) to the current time in seconds .
- Examples
<?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/>";
?>
microtime : Returns the current
Unix
timestamp with microseconds
- Scenes
Get current date and time or a specific date and time stamp, commonly used in the dot analysis program is running, it may be used to convert the date and time of each other.
- Explanation
The current Unix
timestamp, and microseconds, this function is supported only in `gettimeofday ()` `available under the operating system calls.
- Examples
<?php
// 设置当前时区为上海时区
date_default_timezone_set("Asia/Shanghai");
// 获取当前时区
echo "当前时区 : ".date_default_timezone_get()."<br/>";
// 当前日期时间戳
echo "当前日期时间戳: ".time()." <--> ".microtime()." <--> ".microtime(TRUE)."<br/>";
?>
mktime : 取得一个日期的
Unix
时间戳
- 场景
获取给定日期的时间戳,按照"时分秒 月日年"
格式依次解析,返回时间戳.
- 说明
根据给出的参数返回 Unix
时间戳.
- 备注
格式 | 说明 | 参数示例 |
---|---|---|
H |
hour 小时数 |
00 到 23 |
i |
minute 分钟数 |
00 到 59 |
s |
second 秒数 |
00 到 59 |
n |
month 月份数 |
01 到 12 |
j |
day 天数 |
01 到 31 |
Y |
year 年份数,可以是两位或四位数字 |
0-69 对应于 2000-2069 ,70-100 对应于 1970-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/>";
?>
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"));
- 示例
<?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/>";
?>
日期时间函总结
日期时间函数库是 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()
: 将字符串形式的日期时间转换成时间戳
Finally, the document that is complete, do not know to go see more, forget what's methods thanks to ide
smart tips like, multi-purpose use will slowly skilled.