php 获取两个日期相隔几周,怎么样计算2个日期之间相差几周
你的位置:
問答吧
-> PHP基礎
-> 問題詳情
怎么樣計算2個日期之間相差幾周
如題
如果2個日期在同一年中的話可以通過date("W",unix時間戳)來計算
但特別麻煩的就是2個日期不是在同一年中,可能2個日期相差幾年
請高手指教
作者: gengle53022
發布時間: 2007-04-18
可以先算相差多少天 然后再算有多少個周啊
作者: 只愛一個人
發布時間: 2007-04-18
$year_f=2300;
$year_n=idate("Y");
if(!$year_n===0)
$count_r=($year_f-($year_n + (4-($year_n % 4))))/4;
else
$count_r=($year_f-$year_n)/4+1;
$week=(((($year_f-$year_n)+1)*365)+$$count_r)/7;
echo $count_r;
?>
不知道這樣行吧
作者: 真愛有限
發布時間: 2007-04-18
CODE:
[Copy to clipboard]
//按標準時間計算
$time1 ='2006-1-1 15:14:12'; //時間1
$time2 =date('Y-m-d H:i:s'); //時間2
echo (dateDiff("D",$time1,$time2))." weeks";
function dateDiff($date_part,$start_date,$end_date)
{
$starttimestamp = @strtotime($start_date);
if (-1 === $starttimestamp) {
return false;
}
$endtimestamp = @strtotime($end_date);
if (-1 === $endtimestamp) {
return false;
}
switch(strtoupper(trim($date_part)))
{
case "D"://按天計算,返回天數/7
$starttimestamp = strtotime(date('Y-m-d 00:00:00', $starttimestamp));
$endtimestamp = strtotime(date('Y-m-d 00:00:00', $endtimestamp));
#return intval((($endtimestamp - $starttimestamp) / (60*60*24)));//返回天數
return intval((($endtimestamp - $starttimestamp) / (60*60*24))/7);//返回周/7
break;
default:
return FALSE;
}
}
作者: chinahtml
發布時間: 2007-04-18
先轉化為時間戳,再求差值。
LS的
if (-1 === $starttimestamp) {
return false;
}
這一步是什么作用?
作者: coolsail
發布時間: 2007-04-18
strtotime -- 將任何英文文本的日期時間描述解析為 Unix 時間戳
作者: chinahtml
發布時間: 2007-04-18
查了下手冊,strtotime在PHP 5.1.0 之前此函數在失敗時返回 -1,
5.1.0之后 失敗時返回 FALSE,不再是 -1。
作者: coolsail
發布時間: 2007-04-19
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的php 获取两个日期相隔几周,怎么样计算2个日期之间相差几周的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二进制函数_Go二进制文件逆向分析从基础
- 下一篇: stm32usb做虚拟串口和键盘_关于s