本文目錄一覽:
- 1、php 計算時間差 求某個時間是幾分鐘之前、幾小時之前、幾天之前
- 2、PHP人性化時間顯示,實現多少秒前,多少分鐘
- 3、php格式化時間轉換為多少天,小時,分鐘,秒前
- 4、php中如何判斷程序過了5秒鐘?
php 計算時間差 求某個時間是幾分鐘之前、幾小時之前、幾天之前
php計算時間的應用主要有如下幾個:
echo “br***************用PHP打印出前一天的時間***************br”;
echo date(“Y-m-d “,strtotime(” -1 day”));//昨天
echo ‘br’;
echo date(“Y-m-d “,strtotime(” +1 day”)); //明天
echo “br********************輸出當前時間*********************br”;
echo date(“Y年m月d日 l H:i:s A”); //2011年08月29日 Monday 04:52:25 AM
echo ‘br’;
echo date(“y-n-j D h:i:s a”); //11-8-29 Mon 04:52:25 am
echo ‘br’;
echo date(“Y年n月j日 l G:i:s a”,strtotime(“now”));//2011年8月29日 Monday 7:56:05 am
echo “br*****************兩個日期之間的天數******************br”;
$str1=strtotime(“2007-02-08”);
$str2=strtotime(“now”);
print_r (floor(($str2-$str1)/(3600*24)));
echo “br**********************倒計時*************************br”;
$time1=strtotime(“2012-7-18 17:30:00”);
$time2=strtotime(“now”);
$sec=$time1-$time2;
$year=floor($sec/3600/24/365);//年
$temp=$sec-$year*365*24*3600;
$month=floor($temp/3600/24/30);//月
$temp=$temp-$month*30*24*3600;
$day=floor($temp/3600/24);//日
$temp=$temp-$day*3600*24;
$hour=floor($temp/3600);//小時
$temp=$temp-$hour*3600;
$minute=floor($temp/60);//分
$second=$temp-$minute*60;//秒
echo “距離培訓畢業還有”.$year.”年”.$month.”月”.$day.”天”.$hour.”小時”.$minute.”分”.$second.”秒”;
PHP人性化時間顯示,實現多少秒前,多少分鐘
//人性化時間顯示
function formatTime($time){
$rtime = date(“m-d H:i”,$time);
$htime = date(“H:i”,$time);
$time = time() – $time;
if ($time 60){
$str = ‘剛剛’;
}elseif($time 60 * 60){
$min = floor($time/60);
$str = $min.’分鐘前’;
}elseif($time 60 * 60 * 24){
$h = floor($time/(60*60));
$str = $h.’小時前 ‘;
}elseif($time 60 * 60 * 24 * 3){
$d = floor($time/(60*60*24));
if($d==1){
$str = ‘昨天 ‘.$rtime;
}else{
$str = ‘前天 ‘.$rtime;
}
}else{
$str = $rtime;
}
return $str;
}
php格式化時間轉換為多少天,小時,分鐘,秒前
?php
function mytime($date)
{
$str = ”;
$timer = strtotime($date);
$diff = $_SERVER[‘REQUEST_TIME’] – $timer;
$day = floor($diff / 86400);
$free = $diff % 86400;
if($day 0)
{
return $day.”天前”;
}
else
{
if($free0)
{
$hour = floor($free / 3600);
$free = $free % 3600;
if($hour0)
{
return $hour.”小時前”;
}
else
{
if($free0)
{
$min = floor($free / 60);
$free = $free % 60;
if($min0)
{
return $min.”分鐘前”;
}
else
{
if($free0)
{
return $free.”秒前”;
}
else
{
return ‘剛剛’;
}
}
}
else
{
return ‘剛剛’;
}
}
}
else
{
return ‘剛剛’;
}
}
}
echo mytime(“2017-1-18 10:22:01”);
?
php中如何判斷程序過了5秒鐘?
我想樓主要的是異步結果吧。可以採用記錄時間戳的方式獲得。
?php
$dateStart = time();
$dateEnd = time();
$totalSecond = $dateEnd – $dateStart;
echo ‘共執行了’ . $totalSecond . ‘秒’;
?
如果要全局記錄,可以寫到json。這樣就不會影響請求了。
function GlobalWrite($array_data) {
if(file_put_contents(‘global.json’, json_encode($array_data)) === false){
return false;
}else{
return true;
}
}
function GlobalRead() {
return json_decode(file_get_contents(‘global.json’), true);
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/280507.html