温馨提示×

php中日期相关函数有哪些

PHP
养鱼的猫咪
297
2021-03-24 12:12:42
栏目: 编程语言

php中日期相关函数有哪些

php中日期相关的函数有date_create、date_diff、strtotime、microtime常见的几种

1.date_create函数

date_create函数作用:

php中date_create函数的作用是用于返回一个新的DateTime对象。

date_create函数语法:

date_create(time,timezone);

参数:

time:规定日期时间字符串。

timezone:规定time的时区。

date_create函数使用方法:

例:创建一个DateTime对象,并格式化输出

$date=date_create("2021-03-23");

echo date_format($date,"Y/m/d");

2.date_diff函数

date_diff函数作用:

php中date_diff函数的作用是用于返回两个DateTime对象间的差值。

date_diff函数语法:

date_diff(datetime1,datetime2)

date_diff函数使用方法:

例:计算时间2020-03-23至2021-03-23的差值

$date1=date_create("2020-03-23");

$date2=date_create("2021-03-23");

$diff=date_diff($date1,$date2);

3.strtotime函数

strtotime函数作用:

php中strtotime函数的作用是将字符串的日期时间解析为Unix时间戳。

strtotime函数语法:

strtotime(time,now);

strtotime函数使用方法:

echo(strtotime("next Monday") ;

echo(strtotime("last Sunday"));

4.microtime函数

microtime函数作用:

php中microtime函数的作用是返回当前Unix时间戳的微秒数。

microtime函数语法:

microtime();

microtime函数使用方法:

echo(microtime());

0