Heim  >  Artikel  >  Backend-Entwicklung  >  PHP时间常用操作函数

PHP时间常用操作函数

WBOY
WBOYOriginal
2016-07-25 09:12:28924Durchsuche

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 * return 字符串
  6. */
  7. function getAllDateBetDays($tdate1,$tdate2){
  8. $dateformat = 'Y-m-d';
  9. if( !is_date($tdate1,$dateformat) ) return "日期参数1格式错误";
  10. if( !is_date($tdate2,$dateformat) ) return "日期参数2格式错误";
  11. if( $tdate1>$tdate2 ) return "日期参数2必须大于等于日期参数1";
  12. $days = "'" . $tdate1 . "'";
  13. while( $tdate1 $days .= "'" . date("Y-m-d",strtotime($tdate1)+86400) . "'";
  14. $tdate1 = date("Y-m-d",strtotime($tdate1)+86400);
  15. }
  16. return $days;
  17. }
  18. /** use_demo */
  19. $tdate1 = '2014-02-01';
  20. //$tdate1 = 'asdqwe123123sadsad';
  21. $tdate2 = '2014-02-30';
  22. 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 * return String
  4. */
  5. function gatAllMonBetMons($tmonth1,$tmonth2){
  6. $dateformat = "Y-m";
  7. if( !is_month($tmonth1,$dateformat) ) return "月份参数1格式错误";
  8. if( !is_month($tmonth2,$dateformat) ) return "月份参数2格式错误";
  9. if( $tmonth1>$tmonth2 ) return "月份参数2必须大于等于月份参数1";
  10. $months = "'" . $tmonth1 . "'";
  11. while( $tmonth1 $months .= "'" . date("Y-m",strtotime($tmonth1 . "-01" . "+1 month")) . "'";
  12. $tmonth1 = date("Y-m",strtotime($tmonth1 . "-01" . "+1 month"));
  13. }
  14. return $months;
  15. }
  16. /** use_demo */
  17. $month1 = '2013-01';
  18. $month2 = '2014-02';
  19. 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 $NeedDate[4] = date("Y-m",strtotime( date("Y-m-d",time()) . "-1 month" )) . '-' . $tod_num;
  18. }else{
  19. $NeedDate[4] = date("Y-m",strtotime( date("Y-m-d",time()) . "-1 month" )) . '-' . $lmd_num;
  20. }
  21. $NeedDate[5] = date("Y-m-d",time()+86400);
  22. switch ($needle) {
  23. case 0:
  24. return json_encode($NeedDate);
  25. break;
  26. case 1:
  27. return json_encode($NeedDate[1]);
  28. break;
  29. case 2:
  30. return json_encode($NeedDate[2]);
  31. break;
  32. case 3:
  33. return json_encode($NeedDate[3]);
  34. break;
  35. case 4:
  36. return json_encode($NeedDate[4]);
  37. break;
  38. default:
  39. return false;
  40. break;
  41. }
  42. }
  43. /** use_demo */
  44. var_dump(json_decode(getNeedDate(0),true));
  45. 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 */
  5. function getIntervalDays($tdate1,$tdate2){
  6. $dateformat = 'Y-m-d';
  7. if( !is_date($tdate1,$dateformat) ) return "日期参数1格式错误";
  8. if( !is_date($tdate2,$dateformat) ) return "日期参数2格式错误";
  9. if( $tdate1>$tdate2 ) return "日期参数2必须大于等于日期参数1";
  10. $days = ceil((strtotime($tdate2)-strtotime($tdate1))/86400);
  11. return $days;
  12. }
  13. /** use demo */
  14. $tdate1 = '2014-01-01';
  15. $tdate2 = '2014-01-16';
  16. echo getIntervalDays($tdate1,$tdate2);
复制代码



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn