php日期时间代码,PHP日期计算
為了顯示一個(gè)事件的持續(xù)時(shí)間,我試圖計(jì)算兩個(gè)日期之間的差異。如果事件的持續(xù)時(shí)間為星期五17:00至星期日15:00,則針對(duì)該問題給出的大多數(shù)函數(shù)都將失敗。我的目標(biāo)是找出日期之間的區(qū)別,比如:
date('Ymd',$end)-date('Tmd',$begining);
但這很可能會(huì)失敗,因?yàn)橐荒隂]有99個(gè)月,一個(gè)月沒有99天。我可以將日期字符串轉(zhuǎn)換為unix時(shí)間戳,然后除以60*60*12,但有些日子的小時(shí)數(shù)要么大要么小,有時(shí)會(huì)有一個(gè)閏秒。所以我使用getDate()創(chuàng)建了自己的函數(shù),它返回一個(gè)關(guān)于時(shí)間戳的innformation數(shù)組。
/*
* 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;
}
關(guān)于gregoriantojd()函數(shù),它可能會(huì)工作,但是我有點(diǎn)不安,因?yàn)槲也焕斫馑侨绾喂ぷ鞯摹?/p> 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔為你收集整理的php日期时间代码,PHP日期计算的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab平面问题编程,有限元平面矩形
- 下一篇: php 频繁dom和 文件,性能优化之为