Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find week days date from range of the two dates

Tags:

date

php

$start_date = "2013-05-01";
$last_date = "2013-08-30";

How can I get dates of tuesdays and thursdays between these two dates?

like image 516
yajay Avatar asked May 08 '13 12:05

yajay


People also ask

How do you calculate weeks from two dates?

To calculate the number of weeks between two dates, start by counting the number of days between the start and end date. Then, divide that number by 7 days per week.

How do I calculate weekdays between two dates in Excel?

The NETWORKDAYS Function[1] calculates the number of workdays between two dates in Excel. When using the function, the number of weekends are automatically excluded. It also allows you to skip specified holidays and only count business days. It is categorized in Excel as a Date/Time Function.

How do I extract days between two dates?

Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24) Print the final result using document.

How do I calculate days between ranges in Excel?

To find the number of days between these two dates, you can enter “=B2-B1” (without the quotes into cell B3). Once you hit enter, Excel will automatically calculate the number of days between the two dates entered. Note that Excel recognizes leap years.


1 Answers

<?php
$start    = new DateTime('2013-05-01');
$end      = new DateTime('2013-08-30');
$interval = DateInterval::createFromDateString('1 day');
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) {
    if ($dt->format("N") == 2 || $dt->format("N") == 4) {
        echo $dt->format("l Y-m-d") . "<br>\n";
    }
}

See it in action

What this code does:

  1. Creates a starting date object using DateTime.
  2. Creates a starting date object using DateTime.
  3. Creates a DateInterval object to represent our interval of time to iterate through. In this case 1 day.
  4. Creates a DatePeriod object to manage these objects.
  5. Using DatePeriod, it iterates through the date starting with the starting date and ending at the end date. We use DateTime::format() with the N parameter to get the day number of the week. If the day number of the week is 2 (Tuesday) or 4 (Thursday) echo out it's value.
like image 139
John Conde Avatar answered Oct 28 '22 11:10

John Conde