How to obtain the number of complete weeks in a period of time SegmentFault

Source: Internet
Author: User
How to get the number of complete weeks (from Monday to weekend) in a period of time? What is a better way for php to get the full week in a period of time (from Monday to weekend) is there any better way to count php?

Reply content:

How to obtain the number of complete weeks (Monday to weekend) in a period of time? What is a better method for php?

Amount... Is that complicated?

function get_weekend_days($start, $end) {    $days = (strtotime($end) - strtotime($start)) / 86400;    return floor($days / 7);}echo get_weekend_days('2014-02-03', '2015-12-28');

Update 2016/01/20 13:34:07
Code


  State_date **/function getWeek ($ start_date, $ end_date) {// set the weekly start time Monday-Sunday $ week_start = "Monday "; // set the return time format $ date_format = "Ymd"; $ start_date = strtotime ($ start_date); $ end_date = strtotime ($ end_date); if ($ start_date <= $ end_date) {$ end_date = strtotime ("last ". $ week_start, $ end_date); $ start_date = date ("w", $ start_date) = 1? $ Start_date: strtotime ("next ". $ week_start, $ start_date); $ count_week = ($ end_date-$ start_date)/(7*24*3600); for ($ I = 0; $ I <$ count_week; $ I ++) {$ sd = date ($ date_format, $ start_date); $ ed = strtotime ("+ 6 days", $ start_date ); $ eed = date ($ date_format, $ ed); $ arr [] = array ($ sd, $ eed); $ start_date = strtotime ("+ 1 day ", $ ed); // output the test echo "th ". ($ I + 1 ). "Week
"; Echo" start ". $ sd ."
"; Echo" end ". $ eed ."
";}Return $ arr ;}} var_dump (getWeek (" 20160101 "," 20160120 ");?>

Output

WEEK 1 start 1st end 20160104 week start 20160110 end 2nd array (2) {[0] => array (2) {[0] => string (8) "20160104" [1] => string (8) "20160110"} [1] => array (2) {[0] => string (8) "20160111" [1] => string (8) "20160117 "}}

DatePeriod

Get the start-end date set (touch me) and traverse it. if:

in_array(date('w', strtotime($date)), [0, 6])

Just ++

The landlord modified the problem again. It really crashed.

strtotime('next monday');

Get the next Monday, set the DatePeriod step to 7, and traverse it.

  • Headers (all_duration_seconds-(7-(day_of_week_at_begin-1) * seconds_of_day )),

  • Intermediate modulo (n/seconds_of_week ),

  • End (rest_seconds> 0? 1: 0)

This is moderate.

I am too lazy to change the title. The title is not clear-complete

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.