PHP时间常用操作函数

jopen 10年前

1.判断某一天是星期几

/**   * 判断某一天是星期几   * @param $date 格式'YYYY-mm-dd' 格式出错返回false,正确返回对应日期中星期一到星期天的某一天   */  function get_weekday($date){   $date_arr = explode('-', trim($date));   if(!checkdate(intval($date_arr[1]), intval($date_arr[2]), intval($date_arr[0]))){    return false;   }   switch (date('w',strtotime($date))) {    case '0':     $weekday = '天';     break;    case '1':     $weekday = '一';     break;    case '2':     $weekday = '二';     break;    case '3':     $weekday = '三';     break;    case '4':     $weekday = '四';     break;    case '5':     $weekday = '五';     break;    case '6':     $weekday = '六';     break;    default:     return false;     break;   }   return $weekday;  }  //demo_调用  $day = '2014-02-16';  if(get_weekday($day)){   $test1 = get_weekday($day);   echo '星期' . $test1 . '<br />';  }else{   echo '日期出错';  }

2.判断日期格式是否正确

/**   * 判断日期格式是否正确   * 判断格式 yyyy-mm-dd | yyyy-mm-dd hh:ii:ss   * @param $tdate 要判断日期   * @param $dateformat 要判断的日期格式 "Y-m-d"或"Y-m-d H:i:s"   */  function is_date($tdate,$dateformat="Y-m-d"){   $tdate = trim($tdate);   //不能转换为时间戳   if( !is_numeric(strtotime($tdate)) ) return false;   //判断日期是否存在 && 年月日的格式为 Y-m-d   $tdate_date = explode(" ", $tdate);   $tdate_time = explode("-", $tdate_date[0]);   if(isset($tdate_time[0]))    $year = $tdate_time[0];   else    return false;   if(isset($tdate_time[1]))    $month = $tdate_time[1];   else    return false;   if(isset($tdate_time[2]))    $day = $tdate_time[2];   else    return false;   if( !checkdate($month, $day, $year) ) return false;   //判断日期是否为指定格式   $tmpdate = date($dateformat,strtotime($tdate));   if( $tmpdate==$tdate )    return true;   else    return false;  }  /** use demo */  $tdate = '2014-02-16 11:25:33';  //$tdate = '2014-02-16';  //$tdate = '2014.02.16';  //$tdate = 'asdqwe123123sadsad';  $dateformat = 'Y-m-d';  //$dateformat = "Y-m-d H:i:s";  if( is_date($tdate,$dateformat) ){   echo 'true';  }else{   echo 'false';  }

3.返回两个日期之间的所有天数日期

/**   * 返回两个日期之间的所有天数日期   * 依赖is_date()   * @param $tdate1 $tdate2 必须为'Y-m-d'格式   * $tdate1<=$tdate2   * return 字符串   */  function getAllDateBetDays($tdate1,$tdate2){   $dateformat = 'Y-m-d';   if( !is_date($tdate1,$dateformat) ) return "日期参数1格式错误";   if( !is_date($tdate2,$dateformat) ) return "日期参数2格式错误";   if( $tdate1>$tdate2 ) return "日期参数2必须大于等于日期参数1";   $days = "'" . $tdate1 . "'";   while( $tdate1<$tdate2 ){    $days .= "'" . date("Y-m-d",strtotime($tdate1)+86400) . "'";    $tdate1 = date("Y-m-d",strtotime($tdate1)+86400);   }   return $days;  }  /** use_demo */  $tdate1 = '2014-02-01';  //$tdate1 = 'asdqwe123123sadsad';  $tdate2 = '2014-02-30';  echo getAllDateBetDays($tdate1,$tdate2);

4.判断月份格式是否正确

/**   * 判断月份格式是否正确   * 判断格式 yyyy-mm   * @param $tmonth 要判断月份   * @param $monformat 要判断的月份日期 "Y-m"   */  function is_month($tmonth,$monformat="Y-m"){   $tmonth = trim($tmonth);   //不能转换为时间戳   if( !is_numeric(strtotime($tmonth)) ) return false;   //判断月份是否为指定格式   $tmpmonth = date($monformat,strtotime($tmonth));   if( $tmpmonth==$tmonth )    return true;   else    return false;  }  /** use_demo */  //$month = '02.16';  $month = '2014-02';  $monformat = "Y-m";  if( is_month($month,$monformat) )  //if( is_month($month) )   echo 'true';  else   echo 'false';

5.返回两个月份之间所有月份

/**   * 返回两个月份之间所有月份   * @param $tmonth1 $tmonth2 必须 $tmonth1<$tmonth2   * return String   */  function gatAllMonBetMons($tmonth1,$tmonth2){   $dateformat = "Y-m";   if( !is_month($tmonth1,$dateformat) ) return "月份参数1格式错误";   if( !is_month($tmonth2,$dateformat) ) return "月份参数2格式错误";   if( $tmonth1>$tmonth2 ) return "月份参数2必须大于等于月份参数1";   $months = "'" . $tmonth1 . "'";   while( $tmonth1<$tmonth2 ){    $months .= "'" . date("Y-m",strtotime($tmonth1 . "-01" . "+1 month")) . "'";    $tmonth1 = date("Y-m",strtotime($tmonth1 . "-01" . "+1 month"));   }   return $months;  }  /** use_demo */  $month1 = '2013-01';  $month2 = '2014-02';  echo gatAllMonBetMons($month1,$month2);

6.相对当前时间点日期获取

/**   * 相对当前时间点日期获取   * @param $needle "0":全部日期值 "1":昨天 "2":前天 "3":上周今天 "4":上月今天 "5":明天   * 上月今天,如果上月的天数比这个本月天数少,缺少所在天数,则默认返回上月最后一天   * return 相应日期值json格式   */  function getNeedDate($needle){   $tdate = date("Y-m-d",time());   $NeedDate = array();   $NeedDate[1] = date("Y-m-d",time()-86400);   $NeedDate[2] = date("Y-m-d",time()-86400*2);   $NeedDate[3] = date("Y-m-d",time()-86400*7);   //上月天数   $lmd_num = date("t",strtotime( date("Y-m-d",time()) . "-1 month" ));   //今天为该月第几天   $tod_num = date("j",time());   if($tod_num<=$lmd_num){    $NeedDate[4] = date("Y-m",strtotime( date("Y-m-d",time()) . "-1 month" )) . '-' . $tod_num;   }else{    $NeedDate[4] = date("Y-m",strtotime( date("Y-m-d",time()) . "-1 month" )) . '-' . $lmd_num;   }   $NeedDate[5] = date("Y-m-d",time()+86400);   switch ($needle) {    case 0:     return json_encode($NeedDate);     break;    case 1:     return json_encode($NeedDate[1]);     break;    case 2:     return json_encode($NeedDate[2]);     break;    case 3:     return json_encode($NeedDate[3]);     break;    case 4:     return json_encode($NeedDate[4]);     break;    default:     return false;     break;   }  }  /** use_demo */  var_dump(json_decode(getNeedDate(0),true));  var_dump(json_decode(getNeedDate(4),true));

7.闰年判断

/**   * 闰年判断   * 闰年计算:   * 1.世纪年能被400整除   * 2.普通年能被4整除,但不能被100整除   * @param $year   */  function isBissextile($year){   $year = intval(trim($year));   $preg = "/^\d{4,}$/";   if( !preg_match($preg, $year) )    return false;   if( $year%400==0 ){    return true;   }elseif( $year%4==0 && $year%100!=0 ){    return true;   }else{    return false;   }  }  /** use demo */  $year = '2012';  if( isBissextile($year) )   echo 'true';  else   echo 'false';

8.两个日期之间间隔天数

/**   * 两个日期之间间隔天数   * 依赖 is_date   * @param $tdate1 $tdate2 $tdate1<=$tdate2 dateformat:"Y-m-d"   */  function getIntervalDays($tdate1,$tdate2){   $dateformat = 'Y-m-d';   if( !is_date($tdate1,$dateformat) ) return "日期参数1格式错误";   if( !is_date($tdate2,$dateformat) ) return "日期参数2格式错误";   if( $tdate1>$tdate2 ) return "日期参数2必须大于等于日期参数1";   $days = ceil((strtotime($tdate2)-strtotime($tdate1))/86400);   return $days;  }  /** use demo */  $tdate1 = '2014-01-01';  $tdate2 = '2014-01-16';  echo getIntervalDays($tdate1,$tdate2);