PHP Time Common operation function

Source: Internet
Author: User

1. Determine the day of the week

  1. /**
  2. * Determine the day of the week
  3. * @param $date format ' YYYY-MM-DD ' format error returns false to correctly return a day from Monday to Sunday of 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 = ' VI ';
  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. $test 1 = get_weekday ($day);
  42. Echo ' Week '. $test 1. '
    ';
  43. }else{
  44. Echo ' date error ';
  45. }
Copy Code

2. Determine if the date format is correct

  1. /**
  2. * Determine if the date format is correct
  3. * Judging Format Yyyy-mm-dd | YYYY-MM-DD Hh:ii:ss
  4. * @param $tdate to determine the date
  5. * @param $dateformat to determine the date format "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 timestamp
  10. if (!is_numeric (Strtotime ($tdate))) return false;
  11. Determine if the date exists && month date 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. Determines 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. Returns the date of all days between two dates

    1. /**
    2. * Returns the date of all days between two dates
    3. * Dependent Is_date ()
    4. * @param $tdate 1 $tdate 2 must be in ' y-m-d ' format
    5. * $tdate 1<= $tdate 2
    6. * Return string
    7. */
    8. function Getalldatebetdays ($tdate 1, $tdate 2) {
    9. $dateformat = ' y-m-d ';
    10. if (!is_date ($tdate 1, $dateformat)) return "date parameter 1 format error";
    11. if (!is_date ($tdate 2, $dateformat)) return "date parameter 2 format error";
    12. if ($tdate 1> $tdate 2) return "date parameter 2 must be greater than or equal to date parameter 1";
    13. $days = "'". $tdate 1. "'";
    14. while ($tdate 1< $tdate 2) {
    15. $days. = "'". Date ("Y-m-d", Strtotime ($tdate 1) +86400). "'";
    16. $tdate 1 = date ("y-m-d", Strtotime ($tdate 1) +86400);
    17. }
    18. return $days;
    19. }
    20. /** Use_demo */
    21. $tdate 1 = ' 2014-02-01 ';
    22. $tdate 1 = ' Asdqwe123123sadsad ';
    23. $tdate 2 = ' 2014-02-30 ';
    24. Echo getalldatebetdays ($tdate 1, $tdate 2);
Copy Code

4. Determine if the month format is correct

    1. /**
    2. * Determine if the month format is correct
    3. * Judging Format yyyy-mm
    4. * @param $tmonth to determine the month
    5. * @param $monformat to determine the month date "Y-m"
    6. */
    7. function Is_month ($tmonth, $monformat = "Y-m") {
    8. $tmonth = Trim ($tmonth);
    9. cannot be converted to timestamp
    10. if (!is_numeric (Strtotime ($tmonth))) return false;
    11. Determines whether the month is a 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. Returns all months between two months

    1. /**
    2. * Returns all months between two months
    3. * @param $tmonth 1 $tmonth 2 must $tmonth 1< $tmonth 2
    4. * Return String
    5. */
    6. function Gatallmonbetmons ($tmonth 1, $tmonth 2) {
    7. $dateformat = "Y-m";
    8. if (!is_month ($tmonth 1, $dateformat)) return "month parameter 1 format error";
    9. if (!is_month ($tmonth 2, $dateformat)) return "Month parameter 2 format error";
    10. if ($tmonth 1> $tmonth 2) return "month parameter 2 must be greater than or equal to the month parameter 1";
    11. $months = "'". $tmonth 1. "'";
    12. while ($tmonth 1< $tmonth 2) {
    13. $months. = "'". Date ("Y-m", Strtotime ($tmonth 1. "-01". "+1 Month")). "'";
    14. $tmonth 1 = date ("Y-m", Strtotime ($tmonth 1.) "-01". "+1 Month"));
    15. }
    16. return $months;
    17. }
    18. /** Use_demo */
    19. $month 1 = ' 2013-01 ';
    20. $month 2 = ' 2014-02 ';
    21. Echo gatallmonbetmons ($month 1, $month 2);
Copy Code

6. Date acquired relative to the current point in time

  1. /**
  2. * Relative to current point-in-time date acquisition
  3. * @param $needle "0": All Date Value "1": Yesterday "2": "3": the Day before Yesterday "4": Last month Today "5": Tomorrow
  4. * Last month today, if the number of days in the month is less than this month and the number of days is missing, the default is to return
  5. * Return corresponding date value 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. Days of last month
  14. $LMD _num = Date ("T", Strtotime (Date ("Y-m-d", Time ()). "-1 month"));
  15. Today is the first 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 can be divisible by 400
    5. * 2. Average year can be divisible by 4, but not divisible 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. Number of days between two dates

    1. /**
    2. * Number of days between two dates
    3. * Dependent is_date
    4. * @param $tdate 1 $tdate 2 $tdate 1<= $tdate 2 dateformat: "Y-m-d"
    5. */
    6. function Getintervaldays ($tdate 1, $tdate 2) {
    7. $dateformat = ' y-m-d ';
    8. if (!is_date ($tdate 1, $dateformat)) return "date parameter 1 format error";
    9. if (!is_date ($tdate 2, $dateformat)) return "date parameter 2 format error";
    10. if ($tdate 1> $tdate 2) return "date parameter 2 must be greater than or equal to date parameter 1";
    11. $days = Ceil ((Strtotime ($tdate 2)-strtotime ($tdate 1))/86400);
    12. return $days;
    13. }
    14. /** Use demo */
    15. $tdate 1 = ' 2014-01-01 ';
    16. $tdate 2 = ' 2014-01-16 ';
    17. Echo getintervaldays ($tdate 1, $tdate 2);
Copy Code

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.