Related
Dynamically generating select in a form which works fine for dates less than March 31 but for April 1st and afterwords it is wrong. You can see I am specifying GMT exclusively which worked perfectly at-least for date:March 31.
$today = strtotime("today GMT");
<select name="date">
<option value=<?php echo $d = strtotime('0 day',$today); ?>> <?php echo date('d M, Y', $d).'-'.$d; ?></option>
<option value=<?php echo $d = strtotime('1 day',$today); ?>> <?php echo date('d M, Y', $d).'-'.$d; ?></option>
<option value=<?php echo $d = strtotime('2 day',$today); ?>> <?php echo date('d M, Y', $d).'-'.$d; ?></option>
<-remainings->
</select>
generated code
28 Mar, 2019-1553731200<--Correct March 28, 2019 12:00:00 AM
29 Mar, 2019-1553817600<--Correct
30 Mar, 2019-1553904000<--Correct
31 Mar, 2019-1553990400<--Correct
01 Apr, 2019-1554073200<--Wrong March 31, 2019 11:00:00 PM (this and remainings should be April <nextday>, 2019 12:00:00 AM)
02 Apr, 2019-1554159600<--Wrong April 1, 2019 11:00:00 PM
03 Apr, 2019-1554246000<--Wrong April 2, 2019 11:00:00 PM
04 Apr, 2019-1554332400<--Wrong April 3, 2019 11:00:00 PM
05 Apr, 2019-1554418800<--Wrong April 4, 2019 11:00:00 PM
06 Apr, 2019-1554505200<--Wrong April 5, 2019 11:00:00 PM
Try changing this $today = strtotime("today GMT"); to this
$today = strtotime("today",gmdate('U'));
When I executed this $today = strtotime("today GMT"); on my system (31-Mar-2019 18:00 time zone EDT), the result was 1553990400 30 Mar, 2019 20:00
I read the PHP: Datetime Relative Formats Doc and could find no indication that time zone is used in any format, which is why I tried it with gmdate('U').
This code:
echo "\ngmdate\n";
echo "current date: ",strtotime("today"),"<-- ",date('d M, Y H:i'),"\n";
echo "'today GMT': ",strtotime("today GMT"),"<--",date('d M, Y H:i',strtotime("today GMT")),"\n\n";
$todayGMdate = strtotime("today",gmdate('U'));
echo $todayGMdate,"<-- ",date('d M, Y H:i',$todayGMdate),"\n";
for ($i = 0; $i < 10; $i++) {
$d=strtotime("+$i day",$todayGMdate);
echo date('d M, Y', $d).'-'.$d," <-- ",date('d M, Y H:i',$d),"\n";
}
Produces this result:
gmdate
current date: 1554091200<-- 01 Apr, 2019 10:44
'today GMT': 1554076800<--31 Mar, 2019 20:00
1554091200<-- 01 Apr, 2019 00:00
01 Apr, 2019-1554091200 <-- 01 Apr, 2019 00:00
02 Apr, 2019-1554177600 <-- 02 Apr, 2019 00:00
03 Apr, 2019-1554264000 <-- 03 Apr, 2019 00:00
04 Apr, 2019-1554350400 <-- 04 Apr, 2019 00:00
05 Apr, 2019-1554436800 <-- 05 Apr, 2019 00:00
06 Apr, 2019-1554523200 <-- 06 Apr, 2019 00:00
07 Apr, 2019-1554609600 <-- 07 Apr, 2019 00:00
08 Apr, 2019-1554696000 <-- 08 Apr, 2019 00:00
09 Apr, 2019-1554782400 <-- 09 Apr, 2019 00:00
10 Apr, 2019-1554868800 <-- 10 Apr, 2019 00:00
It seems like strtotime("today GMT") gets start of today in current locale, and then adds the gmt offset.
I suspect this note from the doc is in play:
Note:
Relative statements are always processed after non-relative
statements. This makes "+1 week july 2008" and "july 2008 +1 week"
equivalent.
Exceptions to this rule are: "yesterday", "midnight", "today", "noon"
and "tomorrow". Note that "tomorrow 11:00" and "11:00 tomorrow" are
different. Considering today's date of "July 23rd, 2008" the first one
produces "2008-07-24 11:00" where as the second one produces
"2008-07-24 00:00". The reason for this is that those five statements
directly influence the current time.
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);
can you help me on how to get the weekly range in between years.
For example:
Date Selected: Dec 16 , 2014 to Jan 15,2015
Weekly Range Should be:
Dec 16, 2014 to Dec 21,2014
Dec 22, 2014 to Dec 28,2014
Dec 29, 2014 to Jan 4, 2015
Jan 5, 2015 to Jan 11,2015
Jan 12, 2015 to Jan 15, 2015
There is a similar solution
Get all week start date and end date within a date range in php
But this doesn't work in between years, it gives incorrect weekly range.
try using this code
$p = new DatePeriod(
new DateTime('2014-16-12'),
new DateInterval('P1W'),
new DateTime('2015-15-01')
);
$dates=array();
foreach ($p as $w) {
$date_week= $w->format('Y-m-d');
$date_week_7= strtotime($date_week);
$dates[] =date('Y-m-d',strtotime("+7 day", $date_week_7));
}
$dates array will contain all the week days between the dates
I'm trying to figure out how can I get the the first day of week for the last 6 months and can't get to a working solution.
If I write date("Y-m-d 00:00:00", strtotime("-1 week", date("Y-m-d")); It just subtracts 7 days from the current date; what I want to do is to always return the date of Monday from that week.
Expected result:
2011-8-8 00:00:00
2011-8-1 00:00:00
2011-7-25 00:00:00
2011-7-18 00:00:00
etc
This should do it:
for ($i=0; $i<52/2; $i++)
echo date('Y-m-d', mktime(1, 0, 0, date('m'), date('d')-date('w')-$i*7+1, date('Y'))) . " 00:00:00\n";
it's slightly changed from Mike's Post, who wants the sunday instead of the monday.
I'd recommend DateTime::createFromFormat.
Pre-PHP 5.3, you can use strtotime instead:
<?php
define('NUM_WEEKS', 10);
$dates = Array();
$dates[] = strtotime('Monday');
for ($i = 0; $i < NUM_WEEKS-1; $i++)
$dates[] = strtotime('-1 week', $dates[$i]);
foreach ($dates as $date)
echo strftime('%c', $date) . "\n";
?>
Output:
Mon Aug 22 00:00:00 2011
Mon Aug 15 00:00:00 2011
Mon Aug 8 00:00:00 2011
Mon Aug 1 00:00:00 2011
Mon Jul 25 00:00:00 2011
Mon Jul 18 00:00:00 2011
Mon Jul 11 00:00:00 2011
Mon Jul 4 00:00:00 2011
Mon Jun 27 00:00:00 2011
Mon Jun 20 00:00:00 2011
Live demo.
If you're trying to make Saturday (or any other day for that matter)
the first day of the week to select datasets, here's a good
workaround:
<?php $last_sat=date("z", strtotime("last Saturday"));
$second_last_sat=date("z", strtotime("last Saturday-1 week")); ?>
source: http://www.php.net/manual/en/function.strtotime.php
What you'd probably want is
<?php $last_mon=date("z", strtotime("last Monday "));
$second_last_mon=date("z", strtotime("last Monday-1 week")); ?>
etc..
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);