欢迎光临
我们一直在努力

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)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。