PHP Function for Get All Mondays Within Date Range

How to get the dates for all mondays between two dates but only for whole weeks within the range with PHP?

One possible approach:

function isMonday($date) {
return $date->format('N') === '1';
}

function isSunday($date) {
return $date->format('N') === '7';
}

function getMondays($start, $end) {
$mondays = [];

$datePeriod = new DatePeriod($start, new DateInterval('P1D'), $end);
foreach ($datePeriod as $date) {
if (isMonday($date)) $mondays[] = $date;
}
if (!isSunday($end)) array_pop($mondays);
return $mondays;
}

$startDate = new DateTime('2021-06-20');
$endDate = new DateTime('2021-07-07');
var_dump(getMondays($startDate, $endDate));

3v4l Demo. It's rather direct: there's a DatePeriod Traversable object created from $start and $end dates with 1 day interval, that is iterated with foreach; each date that's a Monday is stored in an array. Last step is discarding the very last item of that array if the endDate is not Sunday.

PHP: Return all dates between two dates in an array

You could also take a look at the DatePeriod class:

$period = new DatePeriod(
new DateTime('2010-10-01'),
new DateInterval('P1D'),
new DateTime('2010-10-05')
);

Which should get you an array with DateTime objects.

To iterate

foreach ($period as $key => $value) {
//$value->format('Y-m-d')
}

How to get monday and wednesday dates of every 2 week between two dates

This looked like a fun challenge.

Here is one way using DateTime objects and the ->modify() function to move to the next Monday and Wednesday.

<?php

$start_date = '01-01-2019';
$end_date = '05-03-2019';

$sd = new DateTimeImmutable($start_date);
$nd = new DateTime($start_date);
$ed = new DateTimeImmutable($end_date);
echo 'Start Date = ' . $sd->format('D Y-m-d').PHP_EOL;

// check if the next date from the start date is a monday or a wednesday
// and output the first date accordingly
if ( $sd->modify('next monday') < $sd->modify('next wednesday')) {
echo '>>>' . $nd->modify('next monday')->format('D d/m/Y'). PHP_EOL;
echo '>>>' . $nd->modify('next wednesday')->format('D d/m/Y'). PHP_EOL;
}else{
echo '>>>' . $nd->modify('next wednesday')->format('D d/m/Y'). PHP_EOL;
}

while (1) {
// add 7 days
$nd->add(new DateInterval('P7D'));
// go to next monday unless that means we went past the end date
if ( $nd->modify('next monday') > $ed ) { break; }
echo '>>>' . $nd->format('D d/m/Y'). PHP_EOL;

// go to next wednesday unless that means we went past the end date
if ( $nd->modify('next wednesday') > $ed ) { break; }
echo '>>>' . $nd->format('D d/m/Y'). PHP_EOL;
}

Results

Start Date = Tue 2019-01-01
>>>Wed 02/01/2019
>>>Mon 14/01/2019
>>>Wed 16/01/2019
>>>Mon 28/01/2019
>>>Wed 30/01/2019
>>>Mon 11/02/2019
>>>Wed 13/02/2019
>>>Mon 25/02/2019
>>>Wed 27/02/2019

how to find number of mondays or tuesdays between two dates?

You could create a function that uses strtotime() recursively to count the number of days. Since strtotime("next monday"); works just fine.

function daycount($day, $startdate, $counter)
{
if($startdate >= time())
{
return $counter;
}
else
{
return daycount($day, strtotime("next ".$day, $startdate), ++$counter);
}
}

echo daycount("monday", strtotime("01.01.2009"), 0);

Hopefully this is something you're looking for :)

Getting all dates for Mondays and Tuesdays for the next year

This is an interesting one. Here's how I'd do it with functions, though it may warrant its own class to really be modular and reusable:

Set up my date formats and excluded dates
define('INTERNAL_FORMAT', 'Y-m-d');
define('DISPLAY_MONTH_FORMAT', 'M Y');
define('DISPLAY_DAY_FORMAT', 'D d M Y');
// format excluded dates as YYYY-MM-DD, date('Y-m-d'):
$excluded_dates = array(
'2010-03-09',
'2010-04-13',
);

Then I need some utility functions to see how the dates run, and what dates are excluded:

// date('w') returns a string numeral as follows:
// '0' Sunday
// '1' Monday
// '2' Tuesday
// '3' Wednesday
// '4' Thursday
// '5' Friday
// '6' Saturday
function isTuesday($date) {
return date('w', strtotime($date)) === '2';
}
function isWednesday($date) {
return date('w', strtotime($date)) === '3';
}

// handle the excluded dates
function isExcludedDate($internal_date) {
global $excluded_dates;
return in_array($internal_date, $excluded_dates);
}

Now we just need to iterate over every day of the next 365 (the next year) and check to see if they're Tuesday or Wednesday and not on the excluded list. We store this in $months_and_dates:

$start_date = date(INTERNAL_FORMAT);

// something to store months and days
$months_and_dates = array();

// loop over 365 days and look for tuesdays or wednesdays not in the excluded list
foreach(range(0,365) as $day) {
$internal_date = date(INTERNAL_FORMAT, strtotime("{$start_date} + {$day} days"));
$this_day = date(DISPLAY_DAY_FORMAT, strtotime($internal_date));
$this_month = date(DISPLAY_MONTH_FORMAT, strtotime($internal_date));
if ((isTuesday($internal_date) || isWednesday($internal_date))
&& !isExcludedDate($internal_date)) {
$months_and_dates[$this_month][] = $this_day;
}
}

You can print_r() it, or to get the display you want, we do this:

foreach($months_and_dates as $month => $days) {
print $month . "<br>";
print implode('<br>', $days);
print "<br>";
}

Here's the result as of today, January 11, 2010:

Jan 2010
Tue 12 Jan 2010
Wed 13 Jan 2010
Tue 19 Jan 2010
Wed 20 Jan 2010
Tue 26 Jan 2010
Wed 27 Jan 2010
Feb 2010
Tue 02 Feb 2010
Wed 03 Feb 2010
Tue 09 Feb 2010
Wed 10 Feb 2010
Tue 16 Feb 2010
Wed 17 Feb 2010
Tue 23 Feb 2010
Wed 24 Feb 2010
Mar 2010
Tue 02 Mar 2010
Wed 03 Mar 2010
Wed 10 Mar 2010
Tue 16 Mar 2010
Wed 17 Mar 2010
Tue 23 Mar 2010
Wed 24 Mar 2010
Tue 30 Mar 2010
Wed 31 Mar 2010
Apr 2010
Tue 06 Apr 2010
Wed 07 Apr 2010
Wed 14 Apr 2010
Tue 20 Apr 2010
Wed 21 Apr 2010
Tue 27 Apr 2010
Wed 28 Apr 2010
May 2010
Tue 04 May 2010
Wed 05 May 2010
Tue 11 May 2010
Wed 12 May 2010
Tue 18 May 2010
Wed 19 May 2010
Tue 25 May 2010
Wed 26 May 2010
Jun 2010
Tue 01 Jun 2010
Wed 02 Jun 2010
Tue 08 Jun 2010
Wed 09 Jun 2010
Tue 15 Jun 2010
Wed 16 Jun 2010
Tue 22 Jun 2010
Wed 23 Jun 2010
Tue 29 Jun 2010
Wed 30 Jun 2010
Jul 2010
Tue 06 Jul 2010
Wed 07 Jul 2010
Tue 13 Jul 2010
Wed 14 Jul 2010
Tue 20 Jul 2010
Wed 21 Jul 2010
Tue 27 Jul 2010
Wed 28 Jul 2010
Aug 2010
Tue 03 Aug 2010
Wed 04 Aug 2010
Tue 10 Aug 2010
Wed 11 Aug 2010
Tue 17 Aug 2010
Wed 18 Aug 2010
Tue 24 Aug 2010
Wed 25 Aug 2010
Tue 31 Aug 2010
Sep 2010
Wed 01 Sep 2010
Tue 07 Sep 2010
Wed 08 Sep 2010
Tue 14 Sep 2010
Wed 15 Sep 2010
Tue 21 Sep 2010
Wed 22 Sep 2010
Tue 28 Sep 2010
Wed 29 Sep 2010
Oct 2010
Tue 05 Oct 2010
Wed 06 Oct 2010
Tue 12 Oct 2010
Wed 13 Oct 2010
Tue 19 Oct 2010
Wed 20 Oct 2010
Tue 26 Oct 2010
Wed 27 Oct 2010
Nov 2010
Tue 02 Nov 2010
Wed 03 Nov 2010
Tue 09 Nov 2010
Wed 10 Nov 2010
Tue 16 Nov 2010
Wed 17 Nov 2010
Tue 23 Nov 2010
Wed 24 Nov 2010
Tue 30 Nov 2010
Dec 2010
Wed 01 Dec 2010
Tue 07 Dec 2010
Wed 08 Dec 2010
Tue 14 Dec 2010
Wed 15 Dec 2010
Tue 21 Dec 2010
Wed 22 Dec 2010
Tue 28 Dec 2010
Wed 29 Dec 2010
Jan 2011
Tue 04 Jan 2011
Wed 05 Jan 2011
Tue 11 Jan 2011

PHP Carbon, get all dates between date range?

As of Carbon 1.29 it is possible to do:

$period = CarbonPeriod::create('2018-06-14', '2018-06-20');

// Iterate over the period
foreach ($period as $date) {
echo $date->format('Y-m-d');
}

// Convert the period to an array of dates
$dates = $period->toArray();

See documentation for more details: https://carbon.nesbot.com/docs/#api-period.

how to get sunday date between two date

Give this a try:

$startDate = new DateTime('2016-07-15');
$endDate = new DateTime('2016-07-17');

$sundays = array();

while ($startDate <= $endDate) {
if ($startDate->format('w') == 0) {
$sundays[] = $startDate->format('Y-m-d');
}

$startDate->modify('+1 day');
}

var_dump($sundays);

If you want later to use the DateTime objects instead of the formatted date, then you must use DateTimeImmutable for the $startDate variable:

$startDate = new DateTimeImmutable('2016-07-15');
$endDate = new DateTimeImmutable('2016-07-17');

$sundays = array();

while ($startDate <= $endDate) {
if ($startDate->format('w') == 0) {
$sundays[] = $startDate;
}

$startDate->modify('+1 day');
}

var_dump($sundays);

PHP Mysql query that gets a timeframe of all mondays within a certain date range and puts those different start and end times in a database

I created a way to answer the question however it creates a new one.

The answer is as follows:

for ($args["start_ts"]; $args["start_ts"] < $args["end_ts"]; $args["start_ts"]+=604800) {
$mondays .= "INSERT INTO `#__room_closure`(`id_room`,`start_ts`,`end_ts`) VALUES( ".
$args['id_room'].",".$args["start_ts"].",".($args["start_ts"]+21600).");";
};

this will create an array with the several different queries in a string:

"INSERT INTO `#__room_closure`(`id_room`,`start_ts`,`end_ts`) VALUES( 1,1306900800,1306922400);INSERT INTO `#__room_closure`(`id_room`,`start_ts`,`end_ts`) VALUES( 1,1307505600,1307527200);INSERT INTO `#__room_closure`(`id_room`,`start_ts`,`end_ts`) VALUES( 1,1308110400,1308132000);INSERT INTO `#__room_closure`(`id_room`,`start_ts`,`end_ts`) VALUES( 1,1308715200,1308736800);INSERT INTO `#__room_closure`(`id_room`,`start_ts`,`end_ts`) VALUES( 1,1309320000,1309341600);" 

This solved my question.

However with joomla I cannot put multiple queries in the Query() function. But that is a different story and should be told another time.



Related Topics



Leave a reply



Submit