I am trying to get all weekend dates from Months which I have selected but unfortunately not getting weekend dates please help me how can i get thanks.
$request->flexibles_months
[ "Dec", "Jan", "Feb"]
Controller
public function listing(Request $request)
{
return $months = explode(',', $request->flexibles_months);
// return $months = [ "Dec", "Jan", "Feb"]
foreach ($months as $i =>$monthValue) {
$weekend = new DatePeriod(
new DateTime($monthValue),
DateInterval::createFromDateString(' 1 hour'),
new DateTime('next Friday 2 days')
);
}
foreach ($weekend as $hours) {
$a = $hours->format("Y-M-d");
}
return $a;
// $a = 2021-Dec-18
CodePudding user response:
This might help you, you get back and array of all Weekend days as UNIX-timestamps.
With a simple HTML-Form
<form>
<input name="start" type="date" />
<input name="stop" type="date" />
<input type="submit" value="Get Weekend" />
</form>
and my PHP-Code
<?php
if(empty($_GET['start']) || empty($_GET['stop']))
die('no data');
$list = Weekendlisting(strtotime($_GET['start']), strtotime($_GET['stop']));
if($list === false)
die('No weekend!');
foreach ($list as $day) {
print(date('c = (l)', $day) . "<br />\n");
}
function Weekendlisting(int $start, int $stop) {
$day_add = 86400;
$return = [];
if($start >= $stop)
return false;
do {
$test = date('N', $start); //ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
if($test >= 6)
$return[] = $start;
$start = $start $day_add;
} while ( $stop > $start - $day_add);
return $return;
}
?>
i've got the Result:
2021-12-04T00:00:00 00:00 = (Saturday)
2021-12-05T00:00:00 00:00 = (Sunday)
2021-12-11T00:00:00 00:00 = (Saturday)
2021-12-12T00:00:00 00:00 = (Sunday)
2021-12-18T00:00:00 00:00 = (Saturday)
2021-12-19T00:00:00 00:00 = (Sunday)
2021-12-25T00:00:00 00:00 = (Saturday)
2021-12-26T00:00:00 00:00 = (Sunday)
CodePudding user response:
First of all the first line of your function is a return statement, so at this point the function return to where it was called from. Nothing from the code after this line will be called anyway.
Next thing is, that I not really understand what you want to achieve. I'll describe what I guess you want to do.
You will put in some months (here Dec, Jan, Feb) and the result should be all weekend dates in these months, right? (every Saturday and Sunday)
CodePudding user response:
The following code can be cleaned up a bit, but it works.
// Apr and May included for the edge cases.
$months = array('Dec','Jan','Feb','Apr','May');
function weekends($mths){
// so we only have future weekends need to know 'now'.
$now = array('y'=>date('Y'),'m'=>date('m'));
$wkends;
$d;
$m;
$y;
$maxDays;
$saturday;
$sunday;
foreach($mths as $k => $v){
// is the requested month in the past for this year?
$m = date('m',strtotime($now['y'].'-'.$v.'-01'));
// if so, request next year
$y = ($m < $now['m']) ? ($now['y'] 1) : $now['y'];
// What day is the first of the month, 1 for Monday, 7 for Sunday
$d = date('N',strtotime($y.'-'.$m.'-1'));
$saturday = (7 - $d);
$sunday = ($saturday 1);
// And just how many days in the month
$maxDays = date('t',strtotime($y.'-'.$m.'-1'));
do {
// edge case when Sunday is 1st of month
if ($saturday == 0){
$wkends[$v][] = $y.'-'.$m.'-1 (Sun)';
} else {
$wkends[$v][] = $y.'-'.$m.'-'.$saturday.' (Sat)';
if ($sunday <= $maxDays){
$wkends[$v][] = $y.'-'.$m.'-'.$sunday.' (Sun)';
}
}
$saturday = ($saturday 7);
$sunday = ($sunday 7);
} while ($saturday <= $maxDays);
}
}
$result = weekends($months);
var_dump($results);
And the results:
array(5) {
["Dec"]=> array(8) {
[0]=> string(15) "2021-12-4 (Sat)"
[1]=> string(15) "2021-12-5 (Sun)"
[2]=> string(16) "2021-12-11 (Sat)"
[3]=> string(16) "2021-12-12 (Sun)"
[4]=> string(16) "2021-12-18 (Sat)"
[5]=> string(16) "2021-12-19 (Sun)"
[6]=> string(16) "2021-12-25 (Sat)"
[7]=> string(16) "2021-12-26 (Sun)"
}
["Jan"]=> array(10) {
[0]=> string(15) "2022-01-1 (Sat)"
[1]=> string(15) "2022-01-2 (Sun)"
[2]=> string(15) "2022-01-8 (Sat)"
[3]=> string(15) "2022-01-9 (Sun)"
[4]=> string(16) "2022-01-15 (Sat)"
[5]=> string(16) "2022-01-16 (Sun)"
[6]=> string(16) "2022-01-22 (Sat)"
[7]=> string(16) "2022-01-23 (Sun)"
[8]=> string(16) "2022-01-29 (Sat)"
[9]=> string(16) "2022-01-30 (Sun)"
}
["Feb"]=> array(8) {
[0]=> string(15) "2022-02-5 (Sat)"
[1]=> string(15) "2022-02-6 (Sun)"
[2]=> string(16) "2022-02-12 (Sat)"
[3]=> string(16) "2022-02-13 (Sun)"
[4]=> string(16) "2022-02-19 (Sat)"
[5]=> string(16) "2022-02-20 (Sun)"
[6]=> string(16) "2022-02-26 (Sat)"
[7]=> string(16) "2022-02-27 (Sun)"
}
["Apr"]=> array(9) {
[0]=> string(15) "2022-04-2 (Sat)"
[1]=> string(15) "2022-04-3 (Sun)"
[2]=> string(15) "2022-04-9 (Sat)"
[3]=> string(16) "2022-04-10 (Sun)"
[4]=> string(16) "2022-04-16 (Sat)"
[5]=> string(16) "2022-04-17 (Sun)"
[6]=> string(16) "2022-04-23 (Sat)"
[7]=> string(16) "2022-04-24 (Sun)"
[8]=> string(16) "2022-04-30 (Sat)"
}
["May"]=> array(9) {
[0]=> string(15) "2022-05-1 (Sun)"
[1]=> string(15) "2022-05-7 (Sat)"
[2]=> string(15) "2022-05-8 (Sun)"
[3]=> string(16) "2022-05-14 (Sat)"
[4]=> string(16) "2022-05-15 (Sun)"
[5]=> string(16) "2022-05-21 (Sat)"
[6]=> string(16) "2022-05-22 (Sun)"
[7]=> string(16) "2022-05-28 (Sat)"
[8]=> string(16) "2022-05-29 (Sun)"
}
}