php 与时间有关的函数,php中与时间相关的常用函数有哪些
php中與時(shí)間相關(guān)的常用函數(shù)有:date_default_timezone_set()、date_create()、date_diff()、date_timestamp_get()、strtotime()、microtime()。下面我們通過(guò)代碼來(lái)一一介紹。
代碼示例:<?php
/**
* 設(shè)置時(shí)區(qū)
*/
date_default_timezone_set("Asia/Shanghai");
/**
* 獲取時(shí)區(qū)
*/
echo date_default_timezone_get();
//結(jié)果 UTC
echo "
";
/**
* 添加時(shí)間
*/
$date=date_create("2013-03-15"); //創(chuàng)建一個(gè)DateTime 對(duì)象
date_add($date,date_interval_create_from_date_string("40 month"));//years days
//date_interval_create_from_date_string 從字符串的相關(guān)部分建立一個(gè)DateInterval。
echo date_format($date,"Y-m-d");
//結(jié)果2016-07-15
echo "
";
/**
* 減去時(shí)間
*/
$date=date_create("2013-03-15");
date_sub($date,date_interval_create_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
//2013-02-03
echo "
";
/**
* 獲取兩個(gè)時(shí)區(qū)的差值
*/
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);//返回的是一個(gè)DateInterval對(duì)象
echo "
";var_dump($diff);
// object(DateInterval)#4 (15) {
// ["y"]=>
// int(0)
// ["m"]=>
// int(8)
// ["d"]=>
// int(27)
// ["h"]=>
// int(0)
// ["i"]=>
// int(0)
// ["s"]=>
// int(0)
// ["weekday"]=>
// int(0)
// ["weekday_behavior"]=>
// int(0)
// ["first_last_day_of"]=>
// int(0)
// ["invert"]=>
// int(0)
// ["days"]=>
// int(272)
// ["special_type"]=>
// int(0)
// ["special_amount"]=>
// int(0)
// ["have_weekday_relative"]=>
// int(0)
// ["have_special_relative"]=>
// int(0)
// }
echo "
";
/**
* 獲取當(dāng)前時(shí)間戳
*/
$date=date_create();
echo date_timestamp_get($date) .'
';
$time = time();
echo $time .'
';
echo strtotime("now") .'
';
/*
*獲取今天0點(diǎn)時(shí)間戳
*/
echo strtotime("today").'
';
/**
* 獲取帶微秒的時(shí)間
*/
echo microtime(true);
/*
*獲取指定時(shí)間戳
* mktime(hour,minute,second,month,day,year);
*/
echo "
";
echo mktime(18,30,15,3,15,2019);
/*
*獲取前一天0點(diǎn)時(shí)間戳
*/
echo "
";
echo strtotime('yesterday');
/*
*獲取昨天此時(shí)的時(shí)間戳
*/
echo "
";
echo strtotime('-1 days');
?>
運(yùn)行結(jié)果:Asia/Shanghai
2016-07-15
2013-02-03
object(DateInterval)#4 (15) {
["y"]=>
int(0)
["m"]=>
int(8)
["d"]=>
int(27)
["h"]=>
int(0)
["i"]=>
int(0)
["s"]=>
int(0)
["weekday"]=>
int(0)
["weekday_behavior"]=>
int(0)
["first_last_day_of"]=>
int(0)
["invert"]=>
int(0)
["days"]=>
int(272)
["special_type"]=>
int(0)
["special_amount"]=>
int(0)
["have_weekday_relative"]=>
int(0)
["have_special_relative"]=>
int(0)
}
1591150859
1591150859
1591150859
1591113600
1591150859.0074
1552645815
1591027200
1591064459
如果您想了解更多相關(guān)內(nèi)容,請(qǐng)?jiān)L問(wèn)云海天教程網(wǎng)。
總結(jié)
以上是生活随笔為你收集整理的php 与时间有关的函数,php中与时间相关的常用函数有哪些的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php 编译ext目录下的,PHP编译安
- 下一篇: php 检测密码,php检测密码强度