php日期时间代码,PHP日期计算
為了顯示一個事件的持續時間,我試圖計算兩個日期之間的差異。如果事件的持續時間為星期五17:00至星期日15:00,則針對該問題給出的大多數函數都將失敗。我的目標是找出日期之間的區別,比如:
date('Ymd',$end)-date('Tmd',$begining);
但這很可能會失敗,因為一年沒有99個月,一個月沒有99天。我可以將日期字符串轉換為unix時間戳,然后除以60*60*12,但有些日子的小時數要么大要么小,有時會有一個閏秒。所以我使用getDate()創建了自己的函數,它返回一個關于時間戳的innformation數組。
/*
* datediff($first,$last)
* $first - unix timestamp or string aksepted by strtotime()
* $last - unix timestamp or string aksepted by strtotime()
*
* return - the difference in days betveen the two dates
*/
function datediff($first,$last){
$first = is_numeric($first) ? $first : strtotime($first);
$last = is_numeric($last ) ? $last : strtotime($last );
if ($last
// Can't calculate negative difference in dates
return -1;
}
$first = getdate($first);
$last = getdate($last );
// find the difference in days since the start of the year
$datediff = $last['yday'] - $first['yday'];
// if the years do not match add the appropriate number of days.
$yearCountedFrom = $first['year'];
while($last['year'] != $yearCountedFrom ){
// Take leap years into account
if( $yearCountedFrom % 4 == 0 && $yearCountedFrom != 1900 && $yearCountedFrom != 2100 ){
//leap year
$datediff += 366;
}else{
$datediff += 365;
}
$yearCountedFrom++;
}
return $datediff;
}
關于gregoriantojd()函數,它可能會工作,但是我有點不安,因為我不理解它是如何工作的。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的php日期时间代码,PHP日期计算的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab平面问题编程,有限元平面矩形
- 下一篇: php 频繁dom和 文件,性能优化之为