Common PHP time operation functions

Source: Internet
Author: User
Common PHP time operation functions

1. determine the day of the week

  1. /**
  2. * Determine the day of the week.
  3. * @ Param $ date: 'yyyy-mm-DD'. If an error occurs, false is returned. If yes, a day from Monday to Sunday is returned.
  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 = '1 ';
  16. Break;
  17. Case '2 ':
  18. $ Weekday = '2 ';
  19. Break;
  20. Case '3 ':
  21. $ Weekday = '3 ';
  22. Break;
  23. Case '4 ':
  24. $ Weekday = '4 ';
  25. Break;
  26. Case '5 ':
  27. $ Weekday = '5 ';
  28. Break;
  29. Case '6 ':
  30. $ Weekday = '6 ';
  31. Break;
  32. Default:
  33. Return false;
  34. Break;
  35. }
  36. Return $ weekday;
  37. }
  38. // Demo _ call
  39. $ Day = '2017-02-16 ';
  40. If (get_weekday ($ day )){
  41. $ Test1 = get_weekday ($ day );
  42. Echo 'week'. $ test1 .'
    ';
  43. } Else {
  44. Echo 'date error ';
  45. }

2. determine whether the date format is correct

  1. /**
  2. * Determine whether the date format is correct
  3. * Format: yyyy-mm-dd | yyyy-mm-dd hh: ii: ss
  4. * @ Param $ tdate: 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. // Determine whether the date exists & the format of year, month, and Day 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. // Determine 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 = '2017-02-16 11:25:33 ';
  36. // $ Tdate = '2017-02-16 ';
  37. // $ Tdate = '2017. 123 ';
  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. 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 "date parameter 1 format error ";
  11. If (! Is_date ($ tdate2, $ dateformat) return "date parameter 2 format error ";
  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 = '2017-02-01 ';
  22. // $ Tdate1 = 'asdqwe123123sadsad ';
  23. $ Tdate2 = '2017-02-30 ';
  24. Echo getAllDateBetDays ($ tdate1, $ tdate2 );

4. determine whether the month format is correct

  1. /**
  2. * Determine whether the month format is correct
  3. * The judgment format is yyyy-mm.
  4. * @ Param $ tmonth determine the month
  5. * @ Param $ monformat the date of the month 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 ';

5. return all months between two months.

  1. /**
  2. * Returns all months between two months.
  3. * @ Param $ tmonth1 $ tmon2$ $ tmonth1 <$ tmon22.
  4. * Return String
  5. */
  6. Function gatAllMonBetMons ($ tmonth1, $ tmon2 ){
  7. $ Dateformat = "Y-m ";
  8. If (! Is_month ($ tmonth1, $ dateformat) return "month parameter 1 format error ";
  9. If (! Is_month ($ tmon2, $ dateformat) return "month parameter 2 format error ";
  10. If ($ tmonth1> $ tmon2) return "the month parameter 2 must be greater than or equal to the month parameter 1 ";
  11. $ Months = "'". $ tmonth1 ."'";
  12. While ($ tmonth1 <$ tmon2 ){
  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 = '2017-01 ';
  20. $ Mon22. = '2014-02 ';
  21. Echo gatAllMonBetMons ($ month1, $ mon22 );

6. get the date from the current time point

  1. /**
  2. * Get relative to the current time point date
  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. * If the number of days in the previous month is less than the number of days in the current month and the number of days is missing, the last day of the previous month is returned by default.
  5. * Return the json format of the corresponding date value
  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. // Last month's days
  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 ));

7. leap year judgment

  1. /**
  2. * Leap year judgment
  3. * Leap year calculation:
  4. * 1. the century can be divisible by 400
  5. * 2. an ordinary year can be divisible by four, 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 = '000000 ';
  23. If (isBissextile ($ year ))
  24. Echo 'true ';
  25. Else
  26. Echo 'false ';

8. 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 format error ";
  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 = '2017-01-01 ';
  16. $ Tdate2 = '2017-01-16 ';
  17. Echo getIntervalDays ($ tdate1, $ tdate2 );

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.