php常用函数和表单处理,PHP处理时间的常用函数

date ( string $format [, int $timestamp ] )

格式化一个本地时间/日期。 timestamp 是可选的,默认值为 time()。

U 替换成从一个起始时间(好象是1970年1月1日)以来的秒数

Y 替换成4位的年号.

y 替换成2位的年号.

F 替换成月份的英文全称.

M 替换成月份的英文简称.

m 替换成月份数.

z 替换成从当年1月1日以来的天数.

d 替换成日数.

l 替换成星期几的英文全称.

D 替换成星期几的英文简称.

w 替换成星期几(数字).

H 替换成小时数(24小时制).

h 替换成小时数(12小时制).

i 替换成分钟数.

s 替换成秒数.

A 替换成"AM"或"PM".

a 替换成"am"或"pm".

S 替换成序数字后缀,例如:“st”,“nd”,“rd”,“th”.

date("Y-m-d H:i:s", strtotime("-1 days")) //昨天此刻的时间,明天则是 "1 days",前天则是"-2 days";

date("Y-m-d H:i:s", strtotime("0 days")) //今天此刻的时间,等价于date("Y-m-d H:i:s")

getdate() 取得日期/时间信息。

var_dump(getdate(strtotime('-1 day')));//默认为time()

array(11) {

'seconds' =>

int(16)

'minutes' =>

int(35)

'hours' =>

int(13)

'mday' =>

int(18)

'wday' =>

int(2)

'mon' =>

int(6)

'year' =>

int(2019)

'yday' =>

int(168)

'weekday' =>

string(7) "Tuesday"

'month' =>

string(4) "June"

[0] =>

int(1560864916)

}

date_default_timezone_set() 设定默认时区。

date_default_timezone_get() 返回默认时区。

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

// #1

echo strtotime("now"); // 获取当前时间戳

echo date('Y-m-d H:i:s', strtotime("now"));

// #2

echo strtotime("2015-06-11 10:11:00"); // 获取指定的时间戳

echo date('Y-m-d H:i:s', strtotime("2015-06-11 10:11:00"));

// #3

echo strtotime("3 October 2005"); // 获取指定的时间戳[等同于strtotime("2005-10-03")]

echo date('Y-m-d H:i:s', strtotime("3 October 2005"));

// #4

echo strtotime("+5 hours"); // 当前时间加五个小时 [对比#1]

echo date('Y-m-d H:i:s', strtotime("+5 hours"));

// #5

echo strtotime("+1 day"); // 当前时间加1天 [对比#1]

echo date('Y-m-d H:i:s', strtotime("+1 day"));

// #6

echo strtotime("+2 days"); // 当前时间加多天 名词变复数 [对比#1]

echo date('Y-m-d H:i:s', strtotime("+2 days"));

// #7

echo strtotime("+1 week 3 days 7 hours 5 seconds"); // 当前时间加 1周 3天 7小时 5秒 [对比#1]

echo date('Y-m-d H:i:s', strtotime("+1 week 3 days 7 hours 5 seconds"));

// #8

echo strtotime("next Monday"); // 当前时间下一个周一

echo date('Y-m-d H:i:s', strtotime("next Monday"));

// #9

echo strtotime("last Sunday"); // 当前时间前一个周日

echo date('Y-m-d H:i:s', strtotime("last Sunday"));

// #10

echo strtotime("-1 day",strtotime("2018-07-01 10:11:00")); // 给定时间 减去一天

echo date('Y-m-d H:i:s', strtotime("-1 day",strtotime("2018-07-01 10:11:00")));

mktime(hour,minute,second,month,day,year)

返回一个日期的 Unix时间戳

b2459f7ac206e3acedf14497a7f78a79.png

echo mktime(23,37,0,6,19,2019);//1560987420

strftime() 根据区域设置格式化本地时间/日期

77e6addd2bbbe4587725ff53a0da054d.png

echo(strftime("%B %d %Y, %X %Z", mktime(23,37,0,6,19,2019)));//June 19 2019, 23:37:00 中国标准时间

echo(strftime("%Y. %B %d. %A. %X %Z"));//2019. June 19. Wednesday. 15:53:23 中国标准时间

gettimeofday()返回当前时间信息

// 输出 gettimeofday() 返回的数组

print_r(gettimeofday());

/*

Array

(

[sec] => 1560959851 Unix 纪元以来的秒

[usec] => 567045 微秒

[minuteswest] => 0 格林尼治以西的分

[dsttime] => 0 夏令时修正类型

)

*/

// 输出 gettimeofday() 返回的浮点数

echo gettimeofday(true);//1560959851.5691

microtime()返回当前时间的微秒数0.50587000 1521792423

echo microtime();//0.17682200 1560960072

echo microtime(true);//1560960072.1783

time()返回当前时间的 Unix时间戳

checkdate: 验证日期的正确性。

bool checkdate ( int $month , int $day , int $year )

参数

描述

month

必需。规定月,从 1 到 12 的数字值。

day

必需。规定日,从 1 到 31 的数字值。

year

必需。规定年,从 1 到 32767 的数字值。

var_dump(checkdate(12,31,-400));//false

var_dump(checkdate(2,29,2003));//false

var_dump(checkdate(2,29,2004));//闰年 true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值