code example for PHP to calculate the year, month, week, and day of two date intervals

Source: Internet
Author: User
  1. /**

  2. * Calculates the year, month, week, and day of two date intervals
  3. * Edit bbs.it-home.org
  4. */
  5. function format ($a, $b) {
  6. Check the two date size, the default is small after large, if the former large after the small swap position to ensure that the former small after large
  7. if (Strtotime ($a) >strtotime ($b)) list ($a, $b) =array ($b, $a);
  8. $start = Strtotime ($a);
  9. $stop = Strtotime ($b);
  10. $extend = ($stop-$start)/86400;
  11. $result [' extends '] = $extend;
  12. if ($extend <7) {//if less than 7 days direct return days
  13. $result [' daily '] = $extend;
  14. }elseif ($extend <=31) {///less than 28 days returns the number of weeks, as leap year February satisfies
  15. if ($stop ==strtotime ($a. ' +1 month ')) {
  16. $result [' monthly '] = 1;
  17. }else{
  18. $w = Floor ($extend/7);
  19. $d = ($stop-strtotime ($a. ' + '. $w. ' Week ')/86400;
  20. $result [' weekly '] = $w;
  21. $result [' daily '] = $d;
  22. }
  23. }else{
  24. $y = Floor ($extend/365);
  25. if ($y >=1) {//If more than one year
  26. $start = Strtotime ($a. ' + '. $y. ' year ');
  27. $a = date (' y-m-d ', $start);
  28. Judging whether it really has been a year, if not, then cut
  29. if ($start > $stop) {
  30. $a = date (' y-m-d ', strtotime ($a. '-1 month '));
  31. $m = 11;
  32. $y--;
  33. }
  34. $extend = ($stop-strtotime ($a))/86400;
  35. }
  36. if (Isset ($m)) {
  37. $w = Floor ($extend/7);
  38. $d = $extend-$w *7;
  39. }else{
  40. $m = Isset ($m)? $m: Round ($extend/30);
  41. $stop >=strtotime ($a. ' + '. $m. ' Month ')? $m: $m-;
  42. if ($stop >=strtotime ($a. ' + '. $m. ' Month ')} {
  43. $d = $w = ($stop-strtotime ($a. ' + '. $m. ' Month ')/86400;
  44. $w = Floor ($w/7);
  45. $d = $d-$w *7;
  46. }
  47. }
  48. $result [' yearly '] = $y;
  49. $result [' monthly '] = $m;
  50. $result [' weekly '] = $w;
  51. $result [' daily '] = Isset ($d)? $d: null;
  52. }
  53. Return Array_filter ($result);
  54. }

  55. Print_r (Format (' 2012-10-1 ', ' 2012-12-15 '));

  56. ?>

Copy Code

Output result: Array ([extends]=>75[monthly]=>2[weekly]=>2)

2,php Query the number of weeks in a day and the start date of the corresponding week

    1. /**
    2. * @file
    3. * @version 1.1
    4. */
    5. Gets the week number of a date, the start end time for the week
    6. Private Function Getweekstartendday ($day)
    7. {
    8. $g = strftime ("%u", Strtotime ($day));
    9. Return Array (' Week_num ' =>strftime ("%V", Strtotime ($day)), ' Week_start_day ' =>strftime ('%y-%m-%d ', Strtotime ($ Day)-($g-1) *86400), ' WEEK_START_DAY_CN ' =>strftime ('%y year%m month%d ', Strtotime ($day)-($g-1) *86400), ' week_end_day ' = >strftime ('%y-%m-%d ', Strtotime ($day) + (7-$g) *86400), ' WEEK_END_DAY_CN ' =>strftime ('%Y year%m month%d day ', Strtotime ($ Day) + (7-$g) *86400));
    10. }
    11. ?>
Copy Code
  • Related Article

    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.