php計(jì)算時(shí)間相減差距幾天的方法:1、使用“strtotime("時(shí)間")”語句將兩個(gè)時(shí)間轉(zhuǎn)為時(shí)間戳;2、將兩個(gè)時(shí)間戳相減,得到時(shí)間差;3、將時(shí)間差除以一天的總秒數(shù),并用floor()取整即可,語法“floor(時(shí)間差/86400)”。
本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦
php怎么計(jì)算時(shí)間相減差距幾天
實(shí)現(xiàn)思想:
-
使用strtotime()函數(shù)將兩個(gè)時(shí)間轉(zhuǎn)為時(shí)間戳
-
將兩個(gè)時(shí)間戳相減,得到時(shí)間差
-
將時(shí)間差除以一天的總秒數(shù)(24*60*60=86400)
-
用floor()取整
實(shí)現(xiàn)代碼:
<?php header("Content-type:text/html;charset=utf-8"); //2022年1月1日 19點(diǎn)30分0秒 $time1=strtotime("2022-1-1"); //2022年7月7日 7點(diǎn)30分0秒 $time2=strtotime("2022-7-7"); $diff_seconds = $time2 - $time1; $diff_days = floor($diff_seconds/86400); echo "兩個(gè)時(shí)間相差: ".$diff_days." 天"; ?>
推薦學(xué)習(xí):《PHP視頻教程》