PHP time common operation functions

WBOY
Release: 2016-07-25 09:12:28
Original
958 people have browsed it

1. Determine the day of the week a certain day is

  1. /**
  2. * Determine what day of the week a certain day is
  3. * @param $date format 'YYYY-mm-dd' If the format is wrong, it will return false, if it is correct, it will return a day from Monday to Sunday in the corresponding date
  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 = 'day';
  13. break;
  14. case '1':
  15. $weekday = 'one';
  16. break;
  17. case '2':
  18. $weekday = ' two';
  19. break;
  20. case '3':
  21. $weekday = 'three';
  22. break;
  23. case '4':
  24. $weekday = 'four';
  25. break;
  26. case '5':
  27. $weekday = 'five';
  28. break;
  29. case '6':
  30. $weekday = 'six';
  31. break;
  32. default:
  33. return false;
  34. break;
  35. }
  36. return $weekday;
  37. }
  38. //demo_call
  39. $day = '2014-02-16';
  40. if(get_weekday($day)){
  41. $test1 = get_weekday($day);
  42. echo 'week' . $test1 . '
    ';
  43. }else{
  44. echo 'date error';
  45. }
Copy code

2. Determine whether the date format is correct

  1. /**
  2. * Determine whether the date format is correct
  3. * Determine the format yyyy-mm-dd | yyyy-mm-dd hh:ii:ss
  4. * @param $tdate To determine the date
  5. * @param $dateformat The date format to be determined "Y-m-d" or "Y-m-d H:i:s"
  6. */
  7. function is_date($tdate,$dateformat= "Y-m-d"){
  8. $tdate = trim($tdate);
  9. //Cannot be converted to a timestamp
  10. if( !is_numeric(strtotime($tdate)) ) return false;
  11. //Judge whether the date exists&& year, month and day The format is 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. //Judge whether the date is in the specified format
  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. }
Copy code

3. Return all days between two dates

  1. /**
  2. * Returns all days between two dates
  3. * Depends on is_date()
  4. * @param $tdate1 $tdate2 must be in 'Y-m-d' format
  5. * $tdate1<=$tdate2
  6. * return string
  7. */
  8. function getAllDateBetDays($tdate1,$tdate2){
  9. $dateformat = 'Y-m-d';
  10. if( !is_date($tdate1,$dateformat) ) return "The format of date parameter 1 is wrong";
  11. if( !is_date($tdate2,$dateformat) ) return "The format of date parameter 2 is wrong";
  12. if( $tdate1>$tdate2 ) return "Date parameter 2 must be greater than or equal to date parameter 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);
Copy code

4. Determine whether the month format is correct

  1. /**
  2. * Determine whether the month format is correct
  3. * Determine the format yyyy-mm
  4. * @param $tmonth To determine the month
  5. * @param $monformat The month date to be determined "Y-m"
  6. */
  7. function is_month($tmonth,$monformat="Y-m"){
  8. $tmonth = trim($tmonth);
  9. //Cannot be converted to a timestamp
  10. if( !is_numeric(strtotime($tmonth)) ) return false;
  11. //Determine whether the month is in the specified format
  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';
Copy code

5. Return all months between two months

  1. /**
  2. * Return all months between two months
  3. * @param $tmonth1 $tmonth2 must be $tmonth1<$tmonth2
  4. * return String
  5. */
  6. function gatAllMonBetMons($tmonth1,$tmonth2){
  7. $dateformat = "Y-m";
  8. if( !is_month($ tmonth1,$dateformat) ) return "The format of month parameter 1 is wrong";
  9. if( !is_month($tmonth2,$dateformat) ) return "The format of month parameter 2 is wrong";
  10. if( $tmonth1>$tmonth2 ) return "The month parameter 2 must be greater than or equal to month parameter 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);
Copy code

6. Get the date relative to the current time point

  1. /**
  2. * Get the date relative to the current time point
  3. * @param $needle "0": All date values ​​"1": Yesterday "2": The day before yesterday "3": Last week today "4": Last month today "5": Tomorrow
  4. * Today of the previous month, if the number of days in the previous month is less than the number of days in this month and the number of days is missing, the last day of the previous month will be returned by default
  5. * Return the corresponding date value in json format
  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. //Number of days in the previous month
  14. $lmd_num = date("t",strtotime( date("Y-m-d",time()) . "-1 month" ));
  15. //Today is the day of the month
  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));
Copy code

7. Leap year judgment

  1. /**
  2. * Leap year judgment
  3. * Leap year calculation:
  4. * 1. Century years can be divisible by 400
  5. * 2. Ordinary years can be divisible by 4, but not by 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';
Copy code

8. The number of days between two dates

  1. /**
  2. * Number of days between two dates
  3. * Depends on 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 "Date parameter 1 format error";
  9. if( !is_date($ tdate2,$dateformat) ) return "Date parameter 2 has wrong format";
  10. if( $tdate1>$tdate2 ) return "Date parameter 2 must be greater than or equal to date parameter 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);
Copy code



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template