/** * 判断某一天是星期几 * @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 . '
';}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 * 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 $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 * 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 $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 $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 */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);
复制代码
