PHP時間常用操作函數

jopen 10年前發布 | 18K 次閱讀 PHP PHP開發

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);

 

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!