Skip to main content

Week dates between two dates

To list week's start date & end date between given two dates. It also includes number of dates in every set. It allows you to list only those weeks having total seven days. Here the starting day of the week is Monday & the end day of the week is Sunday.
/*
     * Returns array of week's start & end dates with number of days between those.
     *
     * @param string $start_date
     * @param string $end_date
     * @param boolean $only_full_week
     *
     * @return array
     */
    function getWeekDates($start_date, $end_date, $only_full_week = false)
    {
        $stime = strtotime($start_date);
        $etime = strtotime($end_date);

        $weeks = array();

        $i = 0;
        $j = 1;
        while ($stime <= $etime) {
            if ($i == 0 && $j == 1) {
                $weeks[$i]['start_date'] = date('Y-m-d', $stime);
                $weeks[$i]['end_date'] = date('Y-m-d', $stime);
                $weeks[$i]['count'] = $j;
            } else if (date('N', $stime) == 1) {
                $j = 1;
                $weeks[$i]['start_date'] = date('Y-m-d', $stime);
                $weeks[$i]['end_date'] = date('Y-m-d', $stime);
                $weeks[$i]['count'] = $j;
            }

            if (date('N', $stime) == 7) {
                $weeks[$i]['end_date'] = date('Y-m-d', $stime);
                $weeks[$i]['count'] = $j;

                $i ++;
            }
            $j ++;
            $stime = strtotime('+1 day', $stime);
        }
        if ($only_full_week) {
            foreach ($weeks as $key => $week) {
                if ($week['count'] != 7) {
                    unset($weeks[$key]);
                }
            }
        }

        return array_values($weeks);
    }

Here is the example:

$start_date = '2018-12-23';
$end_date = '2019-01-08';
print_r(getWeekDates($start_date, $end_date));
Output:
Array
(
    [0] => Array
        (
            [start_date] => 2018-12-23
            [end_date] => 2018-12-23
            [count] => 1
        )

    [1] => Array
        (
            [start_date] => 2018-12-24
            [end_date] => 2018-12-30
            [count] => 7
        )

    [2] => Array
        (
            [start_date] => 2018-12-31
            [end_date] => 2019-01-06
            [count] => 7
        )

    [3] => Array
        (
            [start_date] => 2019-01-07
            [end_date] => 2019-01-07
            [count] => 1
        )

)
If we need only full weeks, include the third parameter as 'true'

$start_date = '2018-12-23';
$end_date = '2019-01-08';
print_r(getWeekDates($start_date, $end_date, true));
Output:
Array
(
    [0] => Array
        (
            [start_date] => 2018-12-24
            [end_date] => 2018-12-30
            [count] => 7
        )

    [1] => Array
        (
            [start_date] => 2018-12-31
            [end_date] => 2019-01-06
            [count] => 7
        )

)

Comments

Popular posts from this blog

Multiple Checkboxes Validation

This is a regular problem and solution is hardly available on google. When we use multiple checkboxes of same group (same name with square brackets) and you've to read the values from server side, then below solution will help you. Here I'm using jQuery (https://jquery.com/) and jQuery Validate plugin (https://jqueryvalidation.org/) For an example, I've to ask a user which of the listed book they are interested to know about <form id="BooksForm" method="post" name="BooksForm"> <p>Books you are interested in </p> <input class="Books" id="Book1" name="Books[]" type="checkbox" value="1" /> The Inner Game of Tennis <br /> <input class="Books" id="Book2" name="Books[]" type="checkbox" value="1" /> Monk who sold his ferrari <br /> <input class="Books" id="Book3" name=...

PHP Code Review Guidelines

General  The code works  The code is easy to understand  Follows coding conventions  Names are simple and if possible short  Names are spelt correctly  Names contain units where applicable  There are no usages of magic numbers  No hard coded constants that could possibly change in the future  All variables are in the smallest scope possible  There is no commented out code  There is no dead code (inaccessible at Runtime)  No code that can be replaced with library functions  Variables are not accidentally used with null values  Variables are immutable where possible  Code is not repeated or duplicated  There is an else block for every if clause even if it is empty  No complex/long boolean expressions  No negatively named boolean variables  No empty blocks of code  Ideal data structures are used  Constructors do not accept null/none values  Catch clause...

The 7 Types of Leadership: Inspiring Examples and Insights

Leadership is a multifaceted concept with various styles that cater to different needs and situations. Understanding these styles can help you develop your leadership skills and adapt to diverse scenarios. In this article, we'll explore seven prominent leadership styles, each accompanied by insightful examples from influential authors. Let's dive in! Type 1: Transformational Leadership 🚀 Transformational Leadership: Inspiring Change and Innovation Transformational leadership focuses on inspiring and motivating followers to achieve extraordinary outcomes and, in the process, develop their own leadership capacity. Example from Author: James MacGregor Burns James MacGregor Burns, in his book "Leadership," describes transformational leaders as those who seek to change the status quo by appealing to their followers' values and sense of higher purpose. 🔸 Characteristics: - Inspirational Motivation - Intellectual Stimulation - Individualized Consideration - Idealized I...