Home  >  Article  >  Backend Development  >  PHP date example: get last week, this week, last month, this month, this quarter, last quarter

PHP date example: get last week, this week, last month, this month, this quarter, last quarter

WBOY
WBOYOriginal
2016-07-25 09:12:21784browse

Example, php gets last week, this week, last month, this month, this quarter, and last quarter.

  1. date_default_timezone_set('PRC');
  2. date("Y-m-d H:i:s", strtotime(" +2 year"));
  3. date("Y-m-d H:i:s ", strtotime(" +2 month"));
  4. date("Y-m-d H:i:s", strtotime(" +2 week"));
  5. date("Y-m-d H:i:s", strtotime(" + 2 day"));
  6. date("Y-m-d H:i:s", strtotime(" +2 hour"));
  7. date("Y-m-d H:i:s", strtotime(" +2 minute"));
  8. date("Y-m-d H:i:s", strtotime(" +2 second"));
  9. echo date("Ymd",strtotime("now")), "n";
  10. echo date("Ymd" ,strtotime("-1 week Monday")), "n";
  11. echo date("Ymd",strtotime("-1 week Sunday")), "n";
  12. echo date("Ymd",strtotime(" +0 week Monday")), "n";
  13. echo date("Ymd",strtotime("+0 week Sunday")), "n";
  14. echo "************" Month:";
  15. echo date('n');
  16. echo "************Day of the week:";
  17. echo date("w");
  18. echo "***** ****Days of this month:";
  19. echo date("t");
  20. echo "************";
  21. echo '
    Last week:
    ';
  22. echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y "))),"n";
  23. echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w") +7-7,date("Y"))),"n";
  24. echo '
    This week:
    ';
  25. echo date("Y-m-d H:i:s",mktime(0 , 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))),"n";
  26. echo date("Y-m-d H:i :s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))),"n";
  27. echo '
    Last month:
    ';
  28. echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date(" Y"))),"n";
  29. echo date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y")))," n";
  30. echo '
    This month:
    ';
  31. echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date ("Y"))),"n";
  32. echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date(" Y"))),"n";
  33. $season = ceil((date('n'))/3);//Which quarter is this month?
  34. echo '
    This quarter:
  35. echo date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y'))),"n";
  36. echo date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y" ))),date('Y'))),"n";
  37. $season = ceil((date('n'))/3)-1;//What quarter was the last quarter?
  38. echo '
    Last quarter:
    ';
  39. echo date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y' ))),"n";
  40. echo date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0, 0, 0,$season *3,1,date("Y"))),date('Y'))),"n";
  41. ?>
Copy code


Statement:
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