PHP Get all dates for this week or the last seven days all dates method

Source: Internet
Author: User
This article is about how PHP gets all the dates for this week, or all the dates for the last seven days. Hope to help the needy friends

Get all dates this week:

/** * Get all dates this week */function get_week ($time = ', $format = ' y-m-d ') {  $time = $time! = '? $time: Time ();  Gets the current week  $week = Date (' W ', $time);  $date = [];  for ($i =1; $i <=7; $i + +) {    $date [$i] = Date ($format, strtotime (' + '. $i-$week. ' Days ', $time));  }  return $date;}

Execution Result:

Print_r (Get_week ()); Array (  [1] = 2018-06-18  [2] = 2018-06-19  [3] = 2018-06-20  [4] =  = 2018-06-21 [5] = > 2018-06-22  [6] = 2018-06-23  [7] = 2018-06-24)

Get the date for the last seven days:

/** * Get last seven days all dates */function get_weeks ($time = ', $format = ' y-m-d ') {  $time = $time! = '? $time: Time ();  Combined data  $date = [];  for ($i =1; $i <=7; $i + +) {    $date [$i] = Date ($format, strtotime (' + '. $i-7. ' Days ', $time));  }  return $date;}

Execution Result:

Print_r (Get_weeks ()); Array (  [1] = 2018-06-13  [2] = 2018-06-14  [3] = 2018-06-15  [4] = = 2018-06-16  [5] =& Gt 2018-06-17  [6] = 2018-06-18  [7] = 2018-06-19)

Summarize

The above is a small part of the introduction of PHP to get all the dates of the week or the last seven days all the date method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of the Scripting House website!

Articles you may be interested in:

Comparison of the effects of PHP lcg_value and Mt_rand on generating 0~1 random decimals

How to format a class with ID prefixes via PHP

How to restore print_r processed data to the original array via PHP

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.