Related
I need to output a list of dates (only Mondays and Tuesdays) for the next 12 months from current date like so:
Jan 2010
Tue 12 Jan 2010
Mon 18 Jan 2010
Tue 19 Jan 2010
Mon 25 Jan 2010
Feb 2010
Tue 02 Feb 2010
Mon 08 Feb 2010
Tue 09 Feb 2010
Mon 15 Feb 2010
Tue 16 Feb 2010
Mon 22 Feb 2010
Mar 2010
Tue 09 Mar 2010
Mon 15 Mar 2010
Tue 16 Mar 2010
...
Being new to PHP I figured strtotime and looping over the next 52 weeks is the best way to go.
$blockedDatesInput = "08 Mar 2010,12 Apr 2010"; // dont show these dates
$blockedDates = explode ("," , $blockedDatesInput); // convert to array
$currentMonth = ""; // current month marker
// loop over the next 52 weeks to find Mondays and Tuesdays
for($i=0; $i<=52; $i++){
// build the month header
$monthReference = date("M Y", strtotime('+'.$i.' Week'));
// check if date exists in $blockeddate
if (!in_array(date("d M Y", strtotime('+'.$i.' Monday')), $blockedDates) ||
!in_array(date("d M Y", strtotime('+'.$i.' Tuesday')), $blockedDates) ) {
// check if we have to show a new month
if(strcmp($monthReference, $currentMonth) <> 0){
echo $monthReference.'<br />',"\n";
}else{
// output the dates
echo date("D d M Y", strtotime('+'.$i.' Monday')).'<br />',"\n";
echo date("D d M Y", strtotime('+'.$i.' Tuesday')).'<br />',"\n";
}
$currentMonth = date("M Y", strtotime('+'.$i.' Week'));
}
}
However the output from my code is
Jan 2010
Mon 18 Jan 2010
Tue 12 Jan 2010
Mon 25 Jan 2010
Tue 19 Jan 2010
Feb 2010
Mon 08 Feb 2010
Tue 02 Feb 2010
Mon 15 Feb 2010
Tue 09 Feb 2010
Mon 22 Feb 2010
Tue 16 Feb 2010
Mar 2010
Mon 08 Mar 2010
Tue 02 Mar 2010
Mon 15 Mar 2010
Tue 09 Mar 2010
Mon 22 Mar 2010
Tue 16 Mar 2010
Mon 29 Mar 2010
Tue 23 Mar 2010
As you can see the dates are not in the right order and I am at a loss where I am going wrong here.
Is there a more elegant / simple way to solve this?
Version of PHP used is 5.2.11 and no prospect of going to 5.3 anytime soon :-(
Thanks for your help.
Code below modification as suggested by Aly.
Changed the computer date from Tue, 12/01/2010 to Wed, 13/01/2010 to test the output.
$blockedDatesInput = "08 Mar 2010,12 Apr 2010"; // dont show these dates
$blockedDates = explode ("," , $blockedDatesInput); // convert to array
$currentMonth = ""; // current month marker
// loop over the next 52 weeks to find Mondays and Tuesdays
for($i=0; $i<=52; $i++){
// build the month header
$monthReference = date("M Y", strtotime('+'.$i.' Week'));
// check if date exists in $blockeddate
if (!in_array(date("d M Y", strtotime('+'.$i.' Monday')), $blockedDates) ||
!in_array(date("d M Y", strtotime('+'.$i.' Tuesday')), $blockedDates) ) {
// check if we have to show a new month
if(strcmp($monthReference, $currentMonth) <> 0){
echo $monthReference.'<br />',"\n";
}else{
// output the dates (changed the order as suggested by Aly)
echo date("D d M Y", strtotime('+'.$i.' Tuesday')).'<br />',"\n";
echo date("D d M Y", strtotime('+'.$i.' Monday')).'<br />',"\n";
}
$currentMonth = date("M Y", strtotime('+'.$i.' Week'));
}
}
Output again in the wrong order.
Jan 2010
Tue 19 Jan 2010
Mon 18 Jan 2010
Tue 26 Jan 2010
Mon 25 Jan 2010
Feb 2010
Tue 09 Feb 2010
Mon 08 Feb 2010
Tue 16 Feb 2010
Mon 15 Feb 2010
Tue 23 Feb 2010
Mon 22 Feb 2010
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
in a funny coincidence, because today is monday, it is skipping one monday value, that's why they appear out of order. yesterday it would have worked fine.
i.e. your "+ 0 Monday" is NEXT monday, not today.
you may want to look into the "N" format character for date().
Ok now that your computers date is Wednesday you want to print the Mondays before the Tuesdays as the next Monday is closer to Wednesday than the next Tuesday. So try this:
$blockedDatesInput = "08 Mar 2010,12 Apr 2010"; // dont show these dates
$blockedDates = explode ("," , $blockedDatesInput); // convert to array
$currentMonth = ""; // current month marker
// loop over the next 52 weeks to find Mondays and Tuesdays
for($i=0; $i<=52; $i++){
// build the month header
$monthReference = date("M Y", strtotime('+'.$i.' Week'));
// check if date exists in $blockeddate
if (!in_array(date("d M Y", strtotime('+'.$i.' Monday')), $blockedDates) ||
!in_array(date("d M Y", strtotime('+'.$i.' Tuesday')), $blockedDates) ) {
// check if we have to show a new month
if(strcmp($monthReference, $currentMonth) <> 0){
echo $monthReference.'<br />',"\n";
}else{
// output the dates (changed the order as suggested by Aly)
echo date("D d M Y", strtotime('+'.$i.' Monday')).'<br />',"\n";
echo date("D d M Y", strtotime('+'.$i.' Tuesday')).'<br />',"\n";
}
$currentMonth = date("M Y", strtotime('+'.$i.' Week'));
}
}
<?php
/*
#$listDaysOfWeek list days
0 = Monday
1 = Tuesday
2 = Wednesday
3 = Thursday
4 = Friday
5 = Saturday
6 = Sunday
*/
function DatesFromDaysOnWeek(\Datetime $objDateTime, int $numAddDays , array $listDaysOfWeek)
{
$listDaysOfWeek = array_intersect($listDaysOfWeek, range(0,6) );
sort($listDaysOfWeek);
if(empty($listDaysOfWeek) || $numAddDays < 1)
return [];
$results = [];
while ( $numAddDays > 0)
{
foreach($listDaysOfWeek as $numDay)
{
$infoDate = (object) getdate( $objDateTime->getTimestamp() );
if($numDay != $infoDate->wday)
continue;
array_push($results, $objDateTime->format('Y-m-d') );
$numAddDays--;
}
$objDateTime->add(new \DateInterval('P1D'));
}
return $results;
}
#example:
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [1] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [2] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [3] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [4] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [5] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [6] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [0] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-03-01')) , 100 , [1,2] );
print_r($list);
Hi I have a string which is in a csv format,
Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15 16:30:18 2016
jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016
walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
10.4.0.246,Lee.leviste,112.198.77.139:44324,Thu Dec 15 19:50:26 2016
10.4.0.202,jelozero23,112.198.78.211:32704,Thu Dec 15 19:50:59 2016
10.4.0.250,walangmaypake,112.198.78.167:22756,Thu Dec 15 19:51:00 2016
How can I remove those lines starting from ROUTING TABLE upto the last line and get this output?
Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15 16:30:18 2016
jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016
walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016
Thanks in advance :)
If all you need is to take a string and remove all content after "ROUTING TABLE" then this would work. However it looks like you may want to use it as a CSV after? How are you converting it to a string? Depending on how you are using it you may want to look into converting it into an array from the CSV file and then it would be done slightly different.
<?php
$string = "Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15 16:30:18 2016
jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016
walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
10.4.0.246,Lee.leviste,112.198.77.139:44324,Thu Dec 15 19:50:26 2016
10.4.0.202,jelozero23,112.198.78.211:32704,Thu Dec 15 19:50:59 2016
10.4.0.250,walangmaypake,112.198.78.167:22756,Thu Dec 15 19:51:00 2016";
$string = substr($string, 0, strpos($string, "ROUTING TABLE"));
echo "-------<br>";
echo $string;
echo "<br>-------";
// OUTPUT:
// -------
// Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15 16:30:18 2016 jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016 walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016
// -------
?>
I would like to get a list of dates in the following format for a given year;
array:4 [▼
"04" => "25 Jan 2016 - 31 Jan 2016"
"03" => "18 Jan 2016 - 24 Jan 2016"
"02" => "11 Jan 2016 - 17 Jan 2016"
"01" => "4 Jan 2016 - 10 Jan 2016"
]
The array key shows the week number. The array value shows the dates.
If the year given is equal to the current year then I only want it to show dates up until the current week.
How can I achieve this?
This user defined getDates() function which takes weeknumber and year as input will return the desired result.
<?php
echo "<pre>";
print_r(getDates('4', '2016'));
echo "</pre>";
function getDates($weekNumber, $year) {
$returnArr = array();
for($i=$weekNumber; $i >= 1; $i--) {
$wno = $i<=9 ? "0".$i : $i;
$calStr = $year . "W" . $wno;
$startDate = date('d M Y',strtotime($calStr));
$endDate = date('d M Y', strtotime($startDate. ' + 6 day'));
$weekDays = $startDate . ' - ' . $endDate;
$returnArr[$wno] = $weekDays;
}
return $returnArr;
}
Output
Array
(
[04] => 25 Jan 2016 - 31 Jan 2016
[03] => 18 Jan 2016 - 24 Jan 2016
[02] => 11 Jan 2016 - 17 Jan 2016
[01] => 04 Jan 2016 - 10 Jan 2016
)
SO I have write the code for getting the list of upcoming events in next 7 days and it is working fine and giving me the list of upcoming events.but the probmlem is that it is also giving the event record of past days also. Here is my code :
$today = strtotime("Now");
echo date('Y-m-d :H:i:s',$today)."<br>";
$leadDate = strtotime("+7 day", $today);
echo date('Y-m-d :H:i:s',$leadDate)."<br>";
$rs = mysql_query("SELECT * FROM names WHERE due_date BETWEEN CURDATE() AND '$leadDate'") or die(mysql_error());
echo "<b>Dues for upcoming 7 days</b><br>";
while($row=mysql_fetch_assoc($rs))
{
echo $row['name']." => ".$row['normal_date']."<br>";
}
I have the table :
id| name | due_date | normal_date
|1|name1|1390245240|Mon, 20 Jan 2014 19:14:00 GMT
|2|name2|1390331640|Tue, 21 Jan 2014 19:14:00 GMT
|4|name3|1390418040|Wed, 22 Jan 2014 19:14:00 GMT
|5|name4|1390590840|Fri, 24 Jan 2014 19:14:00 GMT
|6|name5|1390850040|Mon, 27 Jan 2014 19:14:00 GMT
|7|name6|1390936440|Tue, 28 Jan 2014 19:14:00 GMT
|8|name7|1396034040|Fri, 28 Mar 2014 19:14:00 GMT
|9|name8|1398708840|Mon, 28 Apr 2014 18:14:00 GMT
|10|name9|1390158840|Sunday, 19 Jan, 2014
|11|name10|1390072440|Saturday, 18 Jan, 2014
|12|name11|1390176000|20 Jan, 2015
server's today's date is 19 Jan 2014 but it is also giving me the list of 18 Jan 2014 but i am looking for the next 7 days record.and my current script is giving me the following out put:
2014-01-19 :20:44:30
2014-01-26 :20:44:30
Dues for upcoming 7 days
name1 => Mon, 20 Jan 2014 19:14:00 GMT
name2 => Tue, 21 Jan 2014 19:14:00 GMT
name3 => Wed, 22 Jan 2014 19:14:00 GMT
name4 => Fri, 24 Jan 2014 19:14:00 GMT
name9 => Sunday, 19 Jan, 2014
name10 => Saturday, 18 Jan, 2014
name11 => 20 Jan, 2015
So Why the 18 jan 2014 & 20 jan 2014 coming in record, where i am wrong.Please Help.
Thanks in Advance.
Try this code:
$rs = mysql_query("SELECT * FROM names WHERE due_date BETWEEN '$today' AND '$leadDate'") or die(mysql_error());
Instead of the CURDATE function use the $today.
I need to output a list of dates (only Mondays and Tuesdays) for the next 12 months from current date like so:
Jan 2010
Tue 12 Jan 2010
Mon 18 Jan 2010
Tue 19 Jan 2010
Mon 25 Jan 2010
Feb 2010
Tue 02 Feb 2010
Mon 08 Feb 2010
Tue 09 Feb 2010
Mon 15 Feb 2010
Tue 16 Feb 2010
Mon 22 Feb 2010
Mar 2010
Tue 09 Mar 2010
Mon 15 Mar 2010
Tue 16 Mar 2010
...
Being new to PHP I figured strtotime and looping over the next 52 weeks is the best way to go.
$blockedDatesInput = "08 Mar 2010,12 Apr 2010"; // dont show these dates
$blockedDates = explode ("," , $blockedDatesInput); // convert to array
$currentMonth = ""; // current month marker
// loop over the next 52 weeks to find Mondays and Tuesdays
for($i=0; $i<=52; $i++){
// build the month header
$monthReference = date("M Y", strtotime('+'.$i.' Week'));
// check if date exists in $blockeddate
if (!in_array(date("d M Y", strtotime('+'.$i.' Monday')), $blockedDates) ||
!in_array(date("d M Y", strtotime('+'.$i.' Tuesday')), $blockedDates) ) {
// check if we have to show a new month
if(strcmp($monthReference, $currentMonth) <> 0){
echo $monthReference.'<br />',"\n";
}else{
// output the dates
echo date("D d M Y", strtotime('+'.$i.' Monday')).'<br />',"\n";
echo date("D d M Y", strtotime('+'.$i.' Tuesday')).'<br />',"\n";
}
$currentMonth = date("M Y", strtotime('+'.$i.' Week'));
}
}
However the output from my code is
Jan 2010
Mon 18 Jan 2010
Tue 12 Jan 2010
Mon 25 Jan 2010
Tue 19 Jan 2010
Feb 2010
Mon 08 Feb 2010
Tue 02 Feb 2010
Mon 15 Feb 2010
Tue 09 Feb 2010
Mon 22 Feb 2010
Tue 16 Feb 2010
Mar 2010
Mon 08 Mar 2010
Tue 02 Mar 2010
Mon 15 Mar 2010
Tue 09 Mar 2010
Mon 22 Mar 2010
Tue 16 Mar 2010
Mon 29 Mar 2010
Tue 23 Mar 2010
As you can see the dates are not in the right order and I am at a loss where I am going wrong here.
Is there a more elegant / simple way to solve this?
Version of PHP used is 5.2.11 and no prospect of going to 5.3 anytime soon :-(
Thanks for your help.
Code below modification as suggested by Aly.
Changed the computer date from Tue, 12/01/2010 to Wed, 13/01/2010 to test the output.
$blockedDatesInput = "08 Mar 2010,12 Apr 2010"; // dont show these dates
$blockedDates = explode ("," , $blockedDatesInput); // convert to array
$currentMonth = ""; // current month marker
// loop over the next 52 weeks to find Mondays and Tuesdays
for($i=0; $i<=52; $i++){
// build the month header
$monthReference = date("M Y", strtotime('+'.$i.' Week'));
// check if date exists in $blockeddate
if (!in_array(date("d M Y", strtotime('+'.$i.' Monday')), $blockedDates) ||
!in_array(date("d M Y", strtotime('+'.$i.' Tuesday')), $blockedDates) ) {
// check if we have to show a new month
if(strcmp($monthReference, $currentMonth) <> 0){
echo $monthReference.'<br />',"\n";
}else{
// output the dates (changed the order as suggested by Aly)
echo date("D d M Y", strtotime('+'.$i.' Tuesday')).'<br />',"\n";
echo date("D d M Y", strtotime('+'.$i.' Monday')).'<br />',"\n";
}
$currentMonth = date("M Y", strtotime('+'.$i.' Week'));
}
}
Output again in the wrong order.
Jan 2010
Tue 19 Jan 2010
Mon 18 Jan 2010
Tue 26 Jan 2010
Mon 25 Jan 2010
Feb 2010
Tue 09 Feb 2010
Mon 08 Feb 2010
Tue 16 Feb 2010
Mon 15 Feb 2010
Tue 23 Feb 2010
Mon 22 Feb 2010
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
in a funny coincidence, because today is monday, it is skipping one monday value, that's why they appear out of order. yesterday it would have worked fine.
i.e. your "+ 0 Monday" is NEXT monday, not today.
you may want to look into the "N" format character for date().
Ok now that your computers date is Wednesday you want to print the Mondays before the Tuesdays as the next Monday is closer to Wednesday than the next Tuesday. So try this:
$blockedDatesInput = "08 Mar 2010,12 Apr 2010"; // dont show these dates
$blockedDates = explode ("," , $blockedDatesInput); // convert to array
$currentMonth = ""; // current month marker
// loop over the next 52 weeks to find Mondays and Tuesdays
for($i=0; $i<=52; $i++){
// build the month header
$monthReference = date("M Y", strtotime('+'.$i.' Week'));
// check if date exists in $blockeddate
if (!in_array(date("d M Y", strtotime('+'.$i.' Monday')), $blockedDates) ||
!in_array(date("d M Y", strtotime('+'.$i.' Tuesday')), $blockedDates) ) {
// check if we have to show a new month
if(strcmp($monthReference, $currentMonth) <> 0){
echo $monthReference.'<br />',"\n";
}else{
// output the dates (changed the order as suggested by Aly)
echo date("D d M Y", strtotime('+'.$i.' Monday')).'<br />',"\n";
echo date("D d M Y", strtotime('+'.$i.' Tuesday')).'<br />',"\n";
}
$currentMonth = date("M Y", strtotime('+'.$i.' Week'));
}
}
<?php
/*
#$listDaysOfWeek list days
0 = Monday
1 = Tuesday
2 = Wednesday
3 = Thursday
4 = Friday
5 = Saturday
6 = Sunday
*/
function DatesFromDaysOnWeek(\Datetime $objDateTime, int $numAddDays , array $listDaysOfWeek)
{
$listDaysOfWeek = array_intersect($listDaysOfWeek, range(0,6) );
sort($listDaysOfWeek);
if(empty($listDaysOfWeek) || $numAddDays < 1)
return [];
$results = [];
while ( $numAddDays > 0)
{
foreach($listDaysOfWeek as $numDay)
{
$infoDate = (object) getdate( $objDateTime->getTimestamp() );
if($numDay != $infoDate->wday)
continue;
array_push($results, $objDateTime->format('Y-m-d') );
$numAddDays--;
}
$objDateTime->add(new \DateInterval('P1D'));
}
return $results;
}
#example:
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [1] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [2] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [3] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [4] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [5] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [6] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [0] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-03-01')) , 100 , [1,2] );
print_r($list);