获取当前星期的日期范围

程序需要,写了一个获取当前星期的日期范围,也就是从星期一到星期日的日期范围。
function getWeekRange($date){
$ret=array();
$timestamp=strtotime($date);
$w=strftime('%u',$timestamp);
$ret['sdate']=date('Y-m-d 00:00:00',$timestamp-($w-1)*86400);
$ret['edate']=date('Y-m-d 23:59:59',$timestamp+(7-$w)*86400);
return $ret;
}

//author:zhxia 获取指定日期所在月的开始日期与结束日期
function getMonthRange($date){
$ret=array();
$timestamp=strtotime($date);
$mdays=date('t',$timestamp);
$ret['sdate']=date('Y-m-1 00:00:00',$timestamp);
$ret['edate']=date('Y-m-'.$mdays.' 23:59:59',$timestamp);
return $ret;
}


//author:zhxia 以上两个函数的应用
function getFilter($n){
$ret=array();
switch($n){
case 1:// 昨天
$ret['sdate']=date('Y-m-d 00:00:00',strtotime('-1 day'));
$ret['edate']=date('Y-m-d 23:59:59',strtotime('-1 day'));
break;
case 2://本星期
$ret=getWeekRange(date('Y-m-d'));
break;
case 3://上一个星期
$strDate=date('Y-m-d',strtotime('-1 week'));
$ret=getWeekRange($strDate);
break;
case 4: //上上星期
$strDate=date('Y-m-d',strtotime('-2 week'));
$ret=getWeekRange($strDate);
break;
case 5: //本月
$ret=getMonthRange(date('Y-m-d'));
break;
case 6://上月
$strDate=date('Y-m-d',strtotime('-1 month'));
$ret=getMonthRange($strDate);
break;
}
return $ret;
}

 

 

 

 

 

 

//获取当天的星期(1-7)

function GetWeek($times)
{
    $res = date('w', strtotime($times));
    if($res==0)
       $res=7;
    return $res;
}
//获取当天时间
function GetTime($times)
{
    $res = date('H:i', strtotime($times));
    return $res;
}
//获取现在过几月的的时间
function GetMonth($Month,$type='l')
{
    if(!strcmp($type,'b'))
      $res=date("Y-m-d H:i:s",strtotime("-$Month months"));
    if(!strcmp($type,'l'))
      $res=date("Y-m-d H:i:s",strtotime("+$Month months"));
    return $res;
}
//获取当前时间
function GetCurrentDateTime()
{
    $res=date("Y-m-d H:i:s",time());
    return $res;
}
//获取当前时间隔几小时之前或之后的时间
function GetDiffHours($hours,$type='l')
{
  if(!strcmp($type,'b'))
     $res=date("Y-m-d H:i:s",strtotime("-$hours hour"));
  if(!strcmp($type,'l'))
     $res=date("Y-m-d H:i:s",strtotime("+$hours hour"));
  return $res;     
}
//间隔几分钟之前或之后的时间
function GetDiffMinute($Minute,$type='l')
{
  if(!strcmp($type,'b'))
     $res=date("Y-m-d H:i:s",strtotime("-$Minute minute"));
  if(!strcmp($type,'l'))
     $res=date("Y-m-d H:i:s",strtotime("+$Minute minute"));
  return $res;     
}
//间隔几秒之前或之后的时间
function GetDiffSec($sec,$type='l')
{
  if(!strcmp($type,'b'))
     $res=date("Y-m-d H:i:s",strtotime("-$sec second"));
  if(!strcmp($type,'l'))
     $res=date("Y-m-d H:i:s",strtotime("+$sec second"));
  return $res;     
}

//间隔几个星期之前或之后的时间
function GetDiffWeek($Week,$type='l')
{
  if(!strcmp($type,'b'))
     $res=date("Y-m-d H:i:s",strtotime("-$Week week"));
  if(!strcmp($type,'l'))
     $res=date("Y-m-d H:i:s",strtotime("+$Week week"));
  return $res;     
}
// 间隔几天之间的时间
function GetDiffDays($days,$type='l')
{
  if(!strcmp($type,'b'))
     $res=date("Y-m-d H:i:s",strtotime("-$days day"));
  if(!strcmp($type,'l'))
     $res=date("Y-m-d H:i:s",strtotime("+$days day"));
  return $res;     
}
//间隔几年之前或之后的时间
function GetDiffYears($year,$type='l')
{
  if(!strcmp($type,'b'))
     $res=date("Y-m-d H:i:s",strtotime("-$year year"));
  if(!strcmp($type,'l'))
     $res=date("Y-m-d H:i:s",strtotime("+$year year"));
  return $res;     
}

posted @ 2013-05-08 17:01  不再犹豫、  阅读(233)  评论(0编辑  收藏  举报