PHP時間常用操作函數

來源:互聯網
上載者:User

1.判斷某一天是星期幾

  1. /**
  2. * 判斷某一天是星期幾
  3. * @param $date 格式'YYYY-mm-dd' 格式出錯返回false,正確返回對應日期中星期一到星期天的某一天
  4. */
  5. function get_weekday($date){
  6. $date_arr = explode('-', trim($date));
  7. if(!checkdate(intval($date_arr[1]), intval($date_arr[2]), intval($date_arr[0]))){
  8. return false;
  9. }
  10. switch (date('w',strtotime($date))) {
  11. case '0':
  12. $weekday = '天';
  13. break;
  14. case '1':
  15. $weekday = '一';
  16. break;
  17. case '2':
  18. $weekday = '二';
  19. break;
  20. case '3':
  21. $weekday = '三';
  22. break;
  23. case '4':
  24. $weekday = '四';
  25. break;
  26. case '5':
  27. $weekday = '五';
  28. break;
  29. case '6':
  30. $weekday = '六';
  31. break;
  32. default:
  33. return false;
  34. break;
  35. }
  36. return $weekday;
  37. }
  38. //demo_調用
  39. $day = '2014-02-16';
  40. if(get_weekday($day)){
  41. $test1 = get_weekday($day);
  42. echo '星期' . $test1 . '
    ';
  43. }else{
  44. echo '日期出錯';
  45. }
複製代碼

2.判斷日期格式是否正確

  1. /**
  2. * 判斷日期格式是否正確
  3. * 判斷格式 yyyy-mm-dd | yyyy-mm-dd hh:ii:ss
  4. * @param $tdate 要判斷日期
  5. * @param $dateformat 要判斷的日期格式 "Y-m-d"或"Y-m-d H:i:s"
  6. */
  7. function is_date($tdate,$dateformat="Y-m-d"){
  8. $tdate = trim($tdate);
  9. //不能轉換為時間戳記
  10. if( !is_numeric(strtotime($tdate)) ) return false;
  11. //判斷日期是否存在 && 年月日的格式為 Y-m-d
  12. $tdate_date = explode(" ", $tdate);
  13. $tdate_time = explode("-", $tdate_date[0]);
  14. if(isset($tdate_time[0]))
  15. $year = $tdate_time[0];
  16. else
  17. return false;
  18. if(isset($tdate_time[1]))
  19. $month = $tdate_time[1];
  20. else
  21. return false;
  22. if(isset($tdate_time[2]))
  23. $day = $tdate_time[2];
  24. else
  25. return false;
  26. if( !checkdate($month, $day, $year) ) return false;
  27. //判斷日期是否為指定格式
  28. $tmpdate = date($dateformat,strtotime($tdate));
  29. if( $tmpdate==$tdate )
  30. return true;
  31. else
  32. return false;
  33. }
  34. /** use demo */
  35. $tdate = '2014-02-16 11:25:33';
  36. //$tdate = '2014-02-16';
  37. //$tdate = '2014.02.16';
  38. //$tdate = 'asdqwe123123sadsad';
  39. $dateformat = 'Y-m-d';
  40. //$dateformat = "Y-m-d H:i:s";
  41. if( is_date($tdate,$dateformat) ){
  42. echo 'true';
  43. }else{
  44. echo 'false';
  45. }
複製代碼

3.返回兩個日期之間的所有天數日期

  1. /**
  2. * 返回兩個日期之間的所有天數日期
  3. * 依賴is_date()
  4. * @param $tdate1 $tdate2 必須為'Y-m-d'格式
  5. * $tdate1<=$tdate2
  6. * return 字串
  7. */
  8. function getAllDateBetDays($tdate1,$tdate2){
  9. $dateformat = 'Y-m-d';
  10. if( !is_date($tdate1,$dateformat) ) return "日期參數1格式錯誤";
  11. if( !is_date($tdate2,$dateformat) ) return "日期參數2格式錯誤";
  12. if( $tdate1>$tdate2 ) return "日期參數2必須大於等於日期參數1";
  13. $days = "'" . $tdate1 . "'";
  14. while( $tdate1<$tdate2 ){
  15. $days .= "'" . date("Y-m-d",strtotime($tdate1)+86400) . "'";
  16. $tdate1 = date("Y-m-d",strtotime($tdate1)+86400);
  17. }
  18. return $days;
  19. }
  20. /** use_demo */
  21. $tdate1 = '2014-02-01';
  22. //$tdate1 = 'asdqwe123123sadsad';
  23. $tdate2 = '2014-02-30';
  24. echo getAllDateBetDays($tdate1,$tdate2);
複製代碼

4.判斷月份格式是否正確

  1. /**
  2. * 判斷月份格式是否正確
  3. * 判斷格式 yyyy-mm
  4. * @param $tmonth 要判斷月份
  5. * @param $monformat 要判斷的月份日期 "Y-m"
  6. */
  7. function is_month($tmonth,$monformat="Y-m"){
  8. $tmonth = trim($tmonth);
  9. //不能轉換為時間戳記
  10. if( !is_numeric(strtotime($tmonth)) ) return false;
  11. //判斷月份是否為指定格式
  12. $tmpmonth = date($monformat,strtotime($tmonth));
  13. if( $tmpmonth==$tmonth )
  14. return true;
  15. else
  16. return false;
  17. }
  18. /** use_demo */
  19. //$month = '02.16';
  20. $month = '2014-02';
  21. $monformat = "Y-m";
  22. if( is_month($month,$monformat) )
  23. //if( is_month($month) )
  24. echo 'true';
  25. else
  26. echo 'false';
複製代碼

5.返回兩個月份之間所有月份

  1. /**
  2. * 返回兩個月份之間所有月份
  3. * @param $tmonth1 $tmonth2 必須 $tmonth1<$tmonth2
  4. * return String
  5. */
  6. function gatAllMonBetMons($tmonth1,$tmonth2){
  7. $dateformat = "Y-m";
  8. if( !is_month($tmonth1,$dateformat) ) return "月份參數1格式錯誤";
  9. if( !is_month($tmonth2,$dateformat) ) return "月份參數2格式錯誤";
  10. if( $tmonth1>$tmonth2 ) return "月份參數2必須大於等於月份參數1";
  11. $months = "'" . $tmonth1 . "'";
  12. while( $tmonth1<$tmonth2 ){
  13. $months .= "'" . date("Y-m",strtotime($tmonth1 . "-01" . "+1 month")) . "'";
  14. $tmonth1 = date("Y-m",strtotime($tmonth1 . "-01" . "+1 month"));
  15. }
  16. return $months;
  17. }
  18. /** use_demo */
  19. $month1 = '2013-01';
  20. $month2 = '2014-02';
  21. echo gatAllMonBetMons($month1,$month2);
複製代碼

6.相對目前時間點日期擷取

  1. /**
  2. * 相對目前時間點日期擷取
  3. * @param $needle "0":全部日期值 "1":昨天 "2":前天 "3":上周今天 "4":上月今天 "5":明天
  4. * 上月今天,如果上月的天數比這個本月天數少,缺少所在天數,則預設返回上月最後一天
  5. * return 相應日期值json格式
  6. */
  7. function getNeedDate($needle){
  8. $tdate = date("Y-m-d",time());
  9. $NeedDate = array();
  10. $NeedDate[1] = date("Y-m-d",time()-86400);
  11. $NeedDate[2] = date("Y-m-d",time()-86400*2);
  12. $NeedDate[3] = date("Y-m-d",time()-86400*7);
  13. //上月天數
  14. $lmd_num = date("t",strtotime( date("Y-m-d",time()) . "-1 month" ));
  15. //今天為該月第幾天
  16. $tod_num = date("j",time());
  17. if($tod_num<=$lmd_num){
  18. $NeedDate[4] = date("Y-m",strtotime( date("Y-m-d",time()) . "-1 month" )) . '-' . $tod_num;
  19. }else{
  20. $NeedDate[4] = date("Y-m",strtotime( date("Y-m-d",time()) . "-1 month" )) . '-' . $lmd_num;
  21. }
  22. $NeedDate[5] = date("Y-m-d",time()+86400);
  23. switch ($needle) {
  24. case 0:
  25. return json_encode($NeedDate);
  26. break;
  27. case 1:
  28. return json_encode($NeedDate[1]);
  29. break;
  30. case 2:
  31. return json_encode($NeedDate[2]);
  32. break;
  33. case 3:
  34. return json_encode($NeedDate[3]);
  35. break;
  36. case 4:
  37. return json_encode($NeedDate[4]);
  38. break;
  39. default:
  40. return false;
  41. break;
  42. }
  43. }
  44. /** use_demo */
  45. var_dump(json_decode(getNeedDate(0),true));
  46. var_dump(json_decode(getNeedDate(4),true));
複製代碼

7.閏年判斷

  1. /**
  2. * 閏年判斷
  3. * 閏年計算:
  4. * 1.世紀年能被400整除
  5. * 2.普通年能被4整除,但不能被100整除
  6. * @param $year
  7. */
  8. function isBissextile($year){
  9. $year = intval(trim($year));
  10. $preg = "/^\d{4,}$/";
  11. if( !preg_match($preg, $year) )
  12. return false;
  13. if( $year%400==0 ){
  14. return true;
  15. }elseif( $year%4==0 && $year%100!=0 ){
  16. return true;
  17. }else{
  18. return false;
  19. }
  20. }
  21. /** use demo */
  22. $year = '2012';
  23. if( isBissextile($year) )
  24. echo 'true';
  25. else
  26. echo 'false';
複製代碼

8.兩個日期之間間隔天數

  1. /**
  2. * 兩個日期之間間隔天數
  3. * 依賴 is_date
  4. * @param $tdate1 $tdate2 $tdate1<=$tdate2 dateformat:"Y-m-d"
  5. */
  6. function getIntervalDays($tdate1,$tdate2){
  7. $dateformat = 'Y-m-d';
  8. if( !is_date($tdate1,$dateformat) ) return "日期參數1格式錯誤";
  9. if( !is_date($tdate2,$dateformat) ) return "日期參數2格式錯誤";
  10. if( $tdate1>$tdate2 ) return "日期參數2必須大於等於日期參數1";
  11. $days = ceil((strtotime($tdate2)-strtotime($tdate1))/86400);
  12. return $days;
  13. }
  14. /** use demo */
  15. $tdate1 = '2014-01-01';
  16. $tdate2 = '2014-01-16';
  17. echo getIntervalDays($tdate1,$tdate2);
複製代碼

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.