How to get a Monday and Wednesday dates after every second week from given start date and end date
For Example
Start date : 01-01-2019
End Date : 05-03-2019
Monday and Wednesday Every Second Week
So Following dates should be in the list
Wednesday : 02-01-2019
Monday : 14-01-2019
Wednesday : 16-01-2019
Monday : 28-01-2019
Wednesday : 30-01-2019
Monday : 11-02-2019
Wednesday : 13-02-2019
Monday : 25-02-2019
Wednesday : 27-02-2019
$startDate = '01-01-2019';
$endDate = '01-03-2019';
$day_number = '1';
$ofWeek = '2';
getDateForSpecificDayBetweenDates($startDate,$endDate,$day_number,$ofWeek);
function getDateForSpecificDayBetweenDates($startDate,$endDate,$day_number,$ofWeek){
$endDate = strtotime($endDate);
$days=array('1'=>'Monday','2' => 'Tuesday','3' => 'Wednesday','4'=>'Thursday','5' =>'Friday','6' => 'Saturday','7'=>'Sunday');
$date_array = array();
$cnt=0;
for($i = strtotime($days[$day_number], strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i))
{
$weeknumber = weekOfMonth(date('Y-m-d',$i));
echo "Week : ".$weeknumber.' - Date : '.date('Y-m-d',$i)."<br>";
if($weeknumber == $ofWeek){
$date_array[$cnt]['dates']=date('Y-m-d',$i);
$date_array[$cnt]['week']=$weeknumber;
$cnt++;
}
}
echo "<pre>";print_r($date_array);echo "<pre>";
}
function weekOfMonth($date) {
// estract date parts
list($y, $m, $d) = explode('-', date('Y-m-d', strtotime($date)));
// current week, min 1
$w = 1;
// for each day since the start of the month
for ($i = 1; $i <= $d; ++$i) {
// if that day was a sunday and is not the first day of month
if ($i > 1 && date('w', strtotime("$y-$m-$i")) == 0) {
// increment current week
++$w;
}
}
// now return
return $w;
}
i have used developed this code but it gives me only 2 week of month i need every 2nd week after start date
i am unable to find the second week from the range.how to identify dates that its from second week
Any help will be appropriated
You could use DatePeriod
to achieve it:
$begin = new DateTime('2019-01-01');
$end = new DateTime('2019-03-05');
$interval = new DateInterval('P2W');
$period = new DatePeriod($begin, $interval, $end);
$dates = [];
foreach ($period as $date) {
$wednesday = $date->modify('next wednesday');
$monday = (clone $wednesday)->modify('+1 week next monday');
if ($wednesday < $end) {
$dates[] = $wednesday;
}
if ($monday < $end) {
$dates[] = $monday;
}
}
foreach ($dates as $date) {
echo $date->format('D: d-m-Y') . '<br />';
}
The result is:
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
Check it here
<?php
$date1 = new DateTime('01-01-2019');
$date2 = new DateTime('05-03-2019');
$interval = $date1->diff($date2);
$weekday = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'];
while($date1<$date2)
{
$day_of_week = intval($date1->format('w'));
if($day_of_week>=1 && $day_of_week<3)
{
$addDays=3-$day_of_week;
}else{
$addDays=15-$day_of_week;
}
$date1->modify('+' . $addDays . ' day');
$day_of_week = intval($date1->format('w')) - 1;
if($date1<$date2)
{
echo $weekday[$day_of_week] . ' - ' . $date1->format('d-m-Y') . '<br>';
}
}
?>
You try this on php fiddle.
First i got the day off week.
Then checked whether it is monday or tuesday if it is then get diffrence from 3 because wednesday is the third day of the week and add it to the day.
then you have monday which should be second monday after wednesday.
The diffrence between two mondays is 15 days hence subtraction from 15.
Here is the ouput.
wednesday - 02-01-2019
monday - 14-01-2019
wednesday - 16-01-2019
monday - 28-01-2019
wednesday - 30-01-2019
monday - 11-02-2019
wednesday - 13-02-2019
monday - 25-02-2019
wednesday - 27-02-2019
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
Try This function pass two date timestamp:
<?php
function get_date($startDateTime,$repeat_until){
for ($i = $startDateTime; $i <= $repeat_until; $i+=86400) {
$ShowDay_arr = array(1, 3);
$Newdate = $i;
$Numerday=date("N",$Newdate);
if(in_array($Numerday,$ShowDay_arr)){
$day = date("D", $Newdate);
echo "<b> Day : ".$day."</b> (".date("d-m-Y",$Newdate).")";
echo "<br>";
}
}
}
get_date(1559401144,1561906744);
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.