當前位置:首頁 » 編程語言 » php日期天數

php日期天數

發布時間: 2022-08-15 04:14:56

『壹』 php 計算兩個日期相隔多少年,多少月,多少天

1、首先計算2020-02-10和2020-02-01日間隔的天數。使用strtotime:<?php $days = (strtotime('2020-02-10') - strtotime('2020-02-01'))/86400; echo $days;。

『貳』 php 如何用date取得指定月份有多少天

$time = strtotime("2013-02-01");

echo date('t', $time);

你思路是對的,只要把日期格式補完就可以了。

『叄』 php計算兩個日期相差多少天

<?php/** * 求兩個日期之間相差的天數 * (針對1970年1月1日之後,求之前可以採用泰勒公式) * @param string $day1 * @param string $day2 * @return number */function diffBetweenTwoDays ($day1, $day2){ $second1 = strtotime($day1); $second2 = strtotime($day2); if ($second1 < $second2) { $tmp = $second2; $second2 = $second1; $second1 = $tmp; } return ($second1 - $second2) / 86400;}$day1 = "2013-07-27";$day2 = "2013-08-04";$diff = diffBetweenTwoDays($day1, $day2);echo $diff."\n";

『肆』 php里有沒有計算兩個時間相隔的天數的函數

/**
* function:計算兩個日期相隔多少年,多少月,多少天
* param string $date1[格式如:2011-11-5]
* param string $date2[格式如:2012-12-01]
* return array array('年','月','日');
*/
function diffDate($date1,$date2)
{
$datetime1 = new \DateTime($date1);
$datetime2 = new \DateTime($date2);
$interval = $datetime1->diff($datetime2);
$time['y'] = $interval->format('%Y');
$time['m'] = $interval->format('%m');
$time['d'] = $interval->format('%d');
$time['h'] = $interval->format('%H');
$time['i'] = $interval->format('%i');
$time['s'] = $interval->format('%s');
$time['a'] = $interval->format('%a'); // 兩個時間相差總天數
return $time;
}
# 使用實例
$sss = diffDate('2015-12-25 12:30:30', '2015-12-26 15:00:00');
print_r($sss);
# 輸出
Array
(
[y] => 00
[m] => 0
[d] => 1
[h] => 02
[i] => 29
[s] => 30
[a] => 1
)

『伍』 PHP 計算某日是這一年的第幾周

在判斷某一天是哪一年的第幾周的時候,根據採用的國際標准(忘了叫什麼名字了),年首或者年末的那幾天有可能不屬於今年的第一周或者最後一周。

代碼如下:

<?php

echo date("oW",strtotime("20141229"))." ";

echo date("oW",strtotime('20160101'))." ";

?>

(5)php日期天數擴展閱讀

php計算時間段的天數:

$firstday = date("Y-m-d H:i:s",time());//當前日期

$timestamp=strtotime($firstday);//當前日期時間戳

$firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));//上個月開始的日期

$lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));//上個月結束的日期

$stimestamp = strtotime($firstday);

$etimestamp = strtotime($lastday);// 計算日期段內有多少天

$days = ($etimestamp-$stimestamp)/86400+1;// 保存每天日期

$date = array();

for($i=0; $i<$days; $i++){

$date[] = date('Y-m-d', $stimestamp+(86400*$i));

}

『陸』 PHP中怎樣計算兩個日期相差的天數

<?php
/**
*求兩個日期之間相差的天數
*(針對1970年1月1日之後,求之前可以採用泰勒公式)
*@paramstring$day1
*@paramstring$day2
*@returnnumber
*/
functiondiffBetweenTwoDays($day1,$day2)
{
$second1=strtotime($day1);
$second2=strtotime($day2);

if($second1<$second2){
$tmp=$second2;
$second2=$second1;
$second1=$tmp;
}
return($second1-$second2)/86400;
}
$day1="2013-07-27";
$day2="2013-08-04";
$diff=diffBetweenTwoDays($day1,$day2);
echo$diff." ";

『柒』 php根據年月獲取當月天數及日期數組的方法

本文實例講述了php根據年月獲取當月天數及日期數組的方法。分享給大家供大家參考,具體如下:
function
get_day(
$date
)
{
$tem
=
explode('-'
,
$date);
//切割日期
得到年份和月份
$year
=
$tem['0'];
$month
=
$tem['1'];
if(
in_array($month
,
array(
1
,
3
,
5
,
7
,
8
,
01
,
03
,
05
,
07
,
08
,
10
,
12)))
{
//
$text
=
$year.'年的'.$month.'月有31天';
$text
=
'31';
}
elseif(
$month
==
2
)
{
if
(
$year%400
==
0
||
($year%4
==
0
&&
$year%100
!==
0)
)
//判斷是否是閏年
{
//
$text
=
$year.'年的'.$month.'月有29天';
$text
=
'29';
}
else{
//
$text
=
$year.'年的'.$month.'月有28天';
$text
=
'28';
}
}
else{
//
$text
=
$year.'年的'.$month.'月有30天';
$text
=
'30';
}
return
$text;
}
echo
get_day('2016-8-1');
運行結果為:31
改造,返回日期數組:
/**
*
獲取當月天數
*
@param
$date
*
@param
$rtype
1天數
2具體日期數組
*
@return
*/
function
get_day(
$date
,$rtype
=
'1')
{
$tem
=
explode('-'
,
$date);
//切割日期
得到年份和月份
$year
=
$tem['0'];
$month
=
$tem['1'];
if(
in_array($month
,
array(
1
,
3
,
5
,
7
,
8
,
01
,
03
,
05
,
07
,
08
,
10
,
12)))
{
//
$text
=
$year.'年的'.$month.'月有31天';
$text
=
'31';
}
elseif(
$month
==
2
)
{
if
(
$year%400
==
0
||
($year%4
==
0
&&
$year%100
!==
0)
)
//判斷是否是閏年
{
//
$text
=
$year.'年的'.$month.'月有29天';
$text
=
'29';
}
else{
//
$text
=
$year.'年的'.$month.'月有28天';
$text
=
'28';
}
}
else{
//
$text
=
$year.'年的'.$month.'月有30天';
$text
=
'30';
}
if
($rtype
==
'2')
{
for
($i
=
1;
$i
<=
$text
;
$i
++
)
{
$r[]
=
$year."-".$month."-".$i;
}
}
else
{
$r
=
$text;
}
return
$r;
}
var_mp(get_day('2016-8-1','2'));
運行結果如下:
array(31)
{
[0]=>
string(8)
"2016-8-1"
[1]=>
string(8)
"2016-8-2"
[2]=>
string(8)
"2016-8-3"
[3]=>
string(8)
"2016-8-4"
[4]=>
string(8)
"2016-8-5"
[5]=>
string(8)
"2016-8-6"
[6]=>
string(8)
"2016-8-7"
[7]=>
string(8)
"2016-8-8"
[8]=>
string(8)
"2016-8-9"
[9]=>
string(9)
"2016-8-10"
[10]=>
string(9)
"2016-8-11"
[11]=>
string(9)
"2016-8-12"
[12]=>
string(9)
"2016-8-13"
[13]=>
string(9)
"2016-8-14"
[14]=>
string(9)
"2016-8-15"
[15]=>
string(9)
"2016-8-16"
[16]=>
string(9)
"2016-8-17"
[17]=>
string(9)
"2016-8-18"
[18]=>
string(9)
"2016-8-19"
[19]=>
string(9)
"2016-8-20"
[20]=>
string(9)
"2016-8-21"
[21]=>
string(9)
"2016-8-22"
[22]=>
string(9)
"2016-8-23"
[23]=>
string(9)
"2016-8-24"
[24]=>
string(9)
"2016-8-25"
[25]=>
string(9)
"2016-8-26"
[26]=>
string(9)
"2016-8-27"
[27]=>
string(9)
"2016-8-28"
[28]=>
string(9)
"2016-8-29"
[29]=>
string(9)
"2016-8-30"
[30]=>
string(9)
"2016-8-31"
}
更多關於PHP相關內容感興趣的讀者可查看本站專題:《php日期與時間用法總結》、《PHP數組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php面向對象程序設計入門教程》、《PHP網路編程技巧總結》、《php字元串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。

『捌』 用php計算給定兩個日期相差多少天

:
計算方法不只下面介紹的這些,只是一些比較常規的方法:
上面的php時間日期函數strtotime已經把字元串日期變成了時間戳,這樣只要讓兩數值相減,然後把秒變成天就可以了,比較的簡單,如下:
$days=round(($enddate-$startdate)/3600/24) ;
下面介紹另外一種方法:
上面判斷的是兩個日期的大小,下面則是判斷生日的程序代碼,得到的$n就是相距生日的天數。
$birthday=生日;
$birthday = preg_replace(‘/d+/’, Date(‘Y’), $birthday, 1);
$d = 60*60*24;
$n= floor((strtotime($birthday)-time())/$d);$n=$n+1;
還有如果相比的是現在的時間,就可以用time()函數,得到的就是現在的時間戳.
第二種情況呢,就是有資料庫,這樣就相對比較容易一些了!如果是MSSQL可以使用觸發器!用專門計算日期差的函數datediff()計算便可!
如果是MYSQL那就用兩個日期欄位的時間戳值,進行計算後便可得到相差的天數了。方法和上面的代碼很像。

『玖』 php計算兩個時間之間的天數

<?php
$days=cal_days_in_month(CAL_GREGORIAN,4,2011);
echo"返回2011-4的天數".$days."<br/>";
$days=date('t',strtotime("2011-4-1"));
echo"返回2011-4的天數".$days."<br/>";
$days=date("t");
echo"當前月的天數".$days."<br/>";

$thisday=date("d",time());


//循環當前天數到當前月底日期
for($i=$thisday;$i<=$days;$i++){
//在這里進行循環,如果跨幾個月的話,就外層再加一個循環月份的就可以了
}
?>

『拾』 php中,計算指定日期還有多少天

思路是先求兩個時間的秒數差,然後將結果轉換即可:

echocalcTime('2018-08-20','2018-08-30');
functioncalcTime($fromTime,$toTime){

//轉時間戳
$fromTime=strtotime($fromTime);
$toTime=strtotime($toTime);
//計算時間差
$newTime=$toTime-$fromTime;
returnround($newTime/86400).'天'.
round($newTime%86400/3600).'小時'.
round($newTime%86400%3600/60).'分鍾';

}
熱點內容
phpnow解壓版 發布:2025-05-16 02:52:49 瀏覽:810
dmporacle資料庫 發布:2025-05-16 02:44:31 瀏覽:831
雲主機上傳 發布:2025-05-16 02:44:30 瀏覽:82
滑鼠如何編程 發布:2025-05-16 02:29:09 瀏覽:816
安卓70能用什麼軟體 發布:2025-05-16 01:45:09 瀏覽:481
編程發展史 發布:2025-05-16 01:38:52 瀏覽:529
android圖片氣泡 發布:2025-05-16 01:38:40 瀏覽:887
文件加密編輯器下載 發布:2025-05-16 01:30:41 瀏覽:344
linuxapacheyum安裝 發布:2025-05-16 01:30:31 瀏覽:477
大連賓利浴池wifi密碼是多少 發布:2025-05-16 01:25:36 瀏覽:172