PHP 日期 时间处理

来自艺搜天下
跳到导航 跳到搜索
//2019-08-15 11:46:47 到2019-08-15
$ptime = "2019-08-15 11:46:47";  # $ctime = date('Y-m-d H:i:s');  
$ptime = date("Y-m-d", strtotime($ptime));  #第二个参数是一个时间戳

比较日期

$ctime = date('Y-m-d');
if(strtotime($ctime) - strtotime($ptime) > 86400){ //大于1天

mktime() 函数可为指定的日期返回时间戳

mktime(hour,minute,second,month,day,year,is_dst)
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "明天是 ".date("Y-m-d", $tomorrow);

获取最近10天,但不包括今天

$start_date = mktime(0, 0, 0, date("m"), date("d")-10, date("Y")); 
$end_date   = mktime(0, 0, 0, date("m"), date("d")-1, date("Y")); 
$start_date = date('Ymd', $start_date); 		   
$end_date   = date('Ymd', $end_date); 	


$end   = "00:01:06.750";
echo strtotime($end); //是一个忽略了毫秒的时间戳,如1565884866  https://tool.lu/timestamp/ 

seconds to "Hour:Minute:Second"

gmdate("H:i:s", 685);  //最好别超过1天86400

HH:MM:SS to seconds

$time = '21:30:10';
$parsed = date_parse($time);
$seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];

参考

https://www.w3school.com.cn/php/php_date.asp