Inserting a loop into a loop? - php

How do I get my output to look like this with the Week number inserted?
Week 1
Monday, January 18, 2016
Wednesday, January 20, 2016
Friday, January 22, 2016
Week 2
Monday, January 25, 2016
Wednesday, January 27, 2016
Friday, January 29, 2016
Week 3
Monday, February 01, 2016
Wednesday, February 03, 2016
Friday, February 05, 2016
Here is my code:
$d = new DateTime('2016-01-17');
$inc = new DateInterval('P1D');
$dateOptions = '';
$required = array(1,3,5);
for ($i=0; $i<28; ++$i) {
$d = $d->add($inc);
if (in_array($d->format('w'), $required)) {
$t = $d->format('l, F d, Y');
echo "$t<br \>";
}
}
Thanks in advance.

$d = new DateTime('2016-01-17');
$inc = new DateInterval('P1D');
$dateOptions = '';
$required = array(1,3,5);
$j = 1;
for ($i = 0; $i <28; ++$i) {
$d = $d->add($inc);
if($d->format('w') == 1){
echo 'Week '.$j.PHP_EOL;
$j++;
}
if (in_array($d->format('w'), $required)) {
$t = $d->format('l, F d, Y');
echo $t.PHP_EOL;
}
}

This should do what you want...
$d = new DateTime('2016-01-17');
$inc = new DateInterval('P1D');
$dateOptions = '';
$required = array(1,3,5);
$week = null;
$week_no = 0;
for ($i=0; $i<28; ++$i) {
$d = $d->add($inc);
if (in_array($d->format('w'), $required)) {
if($week!=$d->format('W')){
$week_no++;
$week = $d->format('W');
echo "Week $week_no<br \>";
}
$t = $d->format('l, F d, Y');
echo "$t<br \>";
}
}
The $week variable keeps track of the dates Week number (from the PHP date() function).
The $week_no keeps track of your incremental number of weeks starting at 1.
The if statement check to see if the the current week number is different to the previous weeks ($week). If it's different it adds 1 to your $week_no a is different to the previous

You can do something like this:
$d = new DateTime('2016-01-17');
$inc = new DateInterval('P1D');
$dateOptions = '';
$required = array(1,3,5);
$counter = $week = 0;
for ($i=0; $i<28; ++$i){
$d = $d->add($inc);
if (in_array($d->format('w'), $required)) {
if($counter % 3 == 0){
echo "Week " . ($week + 1) . "<br />";
++$week;
}
$t = $d->format('l, F d, Y');
echo $t . "<br />";
++$counter;
}
}
Output:
Week 1
Monday, January 18, 2016
Wednesday, January 20, 2016
Friday, January 22, 2016
Week 2
Monday, January 25, 2016
Wednesday, January 27, 2016
Friday, January 29, 2016
Week 3
Monday, February 01, 2016
Wednesday, February 03, 2016
Friday, February 05, 2016
Week 4
Monday, February 08, 2016
Wednesday, February 10, 2016
Friday, February 12, 2016
$counter will keep track of number of dates being printed and $week will print the week number after every three dates.

check http://php.net/manual/en/function.date.php
W => ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)
you just need to add this if before the date printing
if ($d->format('w') == 1){
echo "Week " . $d->format('W') . "<br \>";
}
something like this will do the trick
$d = new DateTime('2016-01-17');
$inc = new DateInterval('P1D');
$dateOptions = '';
$required = array(1,3,5);
for ($i=0; $i<28; ++$i) {
$d = $d->add($inc);
if ($d->format('w') == 1){
echo "Week " . $d->format('W') . "<br \>";
}
if (in_array($d->format('w'), $required)) {
$t = $d->format('l, F d, Y');
echo "$t<br \>";
}
}
Output:
Week 01
Monday, January 18, 2016
Wednesday, January 20, 2016
Friday, January 22, 2016
Week 02
Monday, January 25, 2016
Wednesday, January 27, 2016
Friday, January 29, 2016
Week 03
Monday, February 01, 2016
Wednesday, February 03, 2016
Friday, February 05, 2016
Week 04
Monday, February 08, 2016
Wednesday, February 10, 2016
Friday, February 12, 2016

Try with this code.
$d = new DateTime('2016-01-17');
$inc = new DateInterval('P1D');
$dateOptions = '';
$required = array(1,3,5);
$counter = $week = 0;
for ($i=0; $i<21; ++$i){
$d = $d->add($inc);
if (in_array($d->format('w'), $required)) {
if($counter % 3 == 0){
$week_num = $week + 1;
echo "Week $week_num<br />";
++$week;
}
$t = $d->format('l, F d, Y');
echo "$t<br />";
++$counter;
}
}
Output
Week 1
Monday, January 18, 2016
Wednesday, January 20, 2016
Friday, January 22, 2016
Week 2
Monday, January 25, 2016
Wednesday, January 27, 2016
Friday, January 29, 2016
Week 3
Monday, February 01, 2016
Wednesday, February 03, 2016
Friday, February 05, 2016

Related

PHP- get the last 6 months from the current date

I am trying to get the last six months from the current date in PHP. It's a simple problem, but my code is not giving the right result.
for ($j = 0; $j <= 5; $j++) {
echo date("F Y", strtotime(" -$j month"));
echo "<br>";
}
And the output is
March 2018
March 2018
January 2018
December 2017
December 2017
October 2017
I dont understand why March is coming twice.
Because strototime('-1 month') doesn't handle correctly the end of month.
You could use the first day of the current month:
$dt = strtotime(date('Y-m-01'));
for ($j = 0; $j <= 5; $j++) {
echo date("F Y", strtotime(" -$j month", $dt));
echo "<br>";
}
Outputs:
March 2018
February 2018
January 2018
December 2017
November 2017
October 2017

Generating dates in PHP

Here's my code:
$d = new DateTime('2016-07-14');
$inc = new DateInterval('P1D');
$dateOptions = '';
//1=monday 2=tuesday 3=wednesday
$required = array(1,2,3,4);
$counter = $week = 0;
for ($i=0; $i<40; ++$i){
$d = $d->add($inc);
if (in_array($d->format('w'), $required)) {
if($counter % 4 == 0){
echo ($week + 1) . "<br />";
++$week;
}
$t = $d->format('l, F d, Y');
echo $t . "<br />";
++$counter;
}
}
Here's the output:
1
Monday, July 18, 2016
Tuesday, July 19, 2016
Wednesday, July 20, 2016
Thursday, July 21, 2016
2
Monday, July 25, 2016
Tuesday, July 26, 2016
Wednesday, July 27, 2016
Thursday, July 28, 2016
The output I would like looks like this:
1
Thursday, July 14, 2016
2
Monday, July 18, 2016
Tuesday, July 19, 2016
Wednesday, July 20, 2016
Thursday, July 21, 2016
3
Monday, July 25, 2016
Tuesday, July 26, 2016
Wednesday, July 27, 2016
Thursday, July 28, 2016
What changes to the code needs to be made to allow Thursday to output in the first week?
Thanks in advance!
You are incrementing the counter which gets initialized from 0. So change to week number
You are asking to start from given date, but incrementing it inside the loop. So subtract it before the loop
So your modified code:
<?php
$d = new DateTime('2016-07-14');
$inc = new DateInterval('P1D');
$d = $d->sub($inc); // You need the start date from 14
$required = array(1,2,3,4);
$week = 0;
for ($i=0; $i<40; ++$i){
$d = $d->add($inc);
$weekNumber = $d->format('w');
if (in_array($weekNumber, $required)) {
if(!($weekNumber-1) % 4){ //Don't calculate the counter, but the week number
echo (++$week) . "<br />";
}
$t = $d->format('l, F d, Y');
echo $t . "<br />";
}
}
Output:
Thursday, July 14, 2016
1
Monday, July 18, 2016
Tuesday, July 19, 2016
Wednesday, July 20, 2016
Thursday, July 21, 2016
2
Monday, July 25, 2016
Tuesday, July 26, 2016
Wednesday, July 27, 2016
Thursday, July 28, 2016
3
Monday, August 01, 2016
Tuesday, August 02, 2016
Wednesday, August 03, 2016
Thursday, August 04, 2016
4
Monday, August 08, 2016
Tuesday, August 09, 2016
Wednesday, August 10, 2016
Thursday, August 11, 2016
5
Monday, August 15, 2016
Tuesday, August 16, 2016
Wednesday, August 17, 2016
Thursday, August 18, 2016
6
Monday, August 22, 2016
Your Eval
Use below code,
for ($i=0; $i<40; ++$i){
if($i==0)
{
echo ($week + 1) . "<br />";
++$week;
$t = $d->format('l, F d, Y');
echo $t . "<br />";
}
$d = $d->add($inc);
if (in_array($d->format('w'), $required)) {
if($counter % 4 == 0){
echo ($week + 1) . "<br />";
++$week;
}
$t = $d->format('l, F d, Y');
echo $t . "<br />";
++$counter;
}
}
Output:
1
Thursday, July 14, 2016
2
Monday, July 18, 2016
Tuesday, July 19, 2016
Wednesday, July 20, 2016
Thursday, July 21, 2016
3
Monday, July 25, 2016
Tuesday, July 26, 2016
Wednesday, July 27, 2016
Thursday, July 28, 2016
Here's the final code I used:
$d = new DateTime('2016-08-29');
$inc = new DateInterval('P1D');
$d = $d->sub($inc);
$required = array(2,4); //1=monday 2=tuesday 3=wednesday 4=thursday 5=friday
$howmany = count($required);
$week = 0;
for ($i=0; $i<120; ++$i){
$d = $d->add($inc);
if (in_array($d->format('w'), $required)) {
if($counter % $howmany == 0){
echo ($week + 1) . "<br />";
++$week;
}
$t = $d->format('l, F d, Y');
echo $t . "<br />";
++$counter;
}
}
Thanks for the help!

How to get every specific days of week within given date range in PHP?

This give me every monday date in date range.
Question: How to get every monday and friday of week?
$start_date = date('Y-m-d');
$end_date = date('Y-m-d', strtotime($start_date . ' + 1 MONTH'));
for(
$i = strtotime('Monday', strtotime($start_date));
$i <= strtotime($end_date);
$i = strtotime('+1 WEEK', $i)
) {
echo date('Y-m-d', $i). '<br>';
}
My Update:
$my_dates = [];
for(
$i = strtotime($start_date);
$i <= strtotime($end_date);
$i = strtotime('+1 DAY', $i)
) {
if(in_array(date('N', $i), array(1, 5))) {
$my_dates[] = date('Y-m-d', $i);
}
}
var_dump($my_dates);
Have a look at library called When, it's "Date / Calendar recursion library for PHP 5.3+".
Let's say MF schedule for next month:
$now = new DateTime('NOW');
$till = clone $now;
$till->modify('+1 month');
$r = new When();
$r->startDate($now)
->freq("weekly")
->until($till)
->byday(array('MO', 'FR'))
->generateOccurrences();
$occurrences = $r->occurrences;
If I'm not wrong than you can simply use for loop like as
$start = "2015-09-01";
$end = date('Y-m-d', strtotime("$start +1 months"));
$period = floor((strtotime($end) - strtotime($start))/(24*60*60));
for($i = 0; $i < $period; $i++){
if(in_array(date('l',strtotime("$start +$i day")),["Monday","Friday"]))
echo date('l d M, Y',strtotime("$start +$i day"))."\n";
}
Output:
Friday 04 Sep, 2015
Monday 07 Sep, 2015
Friday 11 Sep, 2015
Monday 14 Sep, 2015
Friday 18 Sep, 2015
Monday 21 Sep, 2015
Friday 25 Sep, 2015
Monday 28 Sep, 2015
Demo

How to loop through months that have been already passed

I have the following to loop through each month of the year. However, it seems to skip February.
$start = new DateTime('2015-01-01');
$start->modify('last day of this month');
$current = new DateTime('now');
$end = new DateTime('2018-01-01');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
$timestamps = array();
foreach ($period as $dt) {
$dt->modify('last day of this month');
echo 'C:' . $current->format('d F Y') . '<br>';
echo 'S:' . $start->format('d F Y') . '<br>';
echo 'D:' . $dt->format('d F Y') . '<br>';
echo '<br><br>';
}
However, the above outputs:
C:17 March 2015
S:31 January 2015
D:31 January 2015
C: 17 March 2015
S:31 January 2015
D:31 March 2015
C: 17 March 2015
S:31 January 2015
D:30 April 2015
Can anyone spot my mistake? I expected the second D to have a value of the 28 February 2015.
I just want a list of months that have already been passed.
Update
The problem highlighted by MLeFevre in the comments is that working with date intervals can be tricky. See Example #3 Beware when adding months http://php.net/manual/en/datetime.add.php.
Rather than use a DatePeriod, why not just use the modify method slightly differently like this:
$current = new DateTime('now');
$end = new DateTime('2018-01-01');
while($current < $end) {
$current->modify('last day of next month');
echo 'C:' . $current->format('d F Y') . '<br>';
}
In your question, you're firstly adding a month, then going to the end of that month. This doesn't work, as the length of each month varies.
Sample output:
C:30 April 2015
C:31 May 2015
C:30 June 2015
C:31 July 2015
C:31 August 2015
C:30 September 2015
C:31 October 2015
C:30 November 2015
C:31 December 2015
C:31 January 2016
C:29 February 2016
C:31 March 2016
// etc.
To loop from $start to $current, you could change the logic slightly like this:
$start = new DateTime('2015-01-31'); // start from end of month
$current = new DateTime('now');
do {
echo 'C:' . $start->format('d F Y') . '<br>';
} while($start->modify('last day of next month') < $current);
Output:
C:31 January 2015
C:28 February 2015
It happen because February has 28 days and your interval is 1 month (30 days). So it skips 30 days from 30 January to 2 March. Then it move to last day of March.
Change
$start->modify('last day of this month');
to
$start->modify('first day of this month');
Your first date is 31-Jan-2015. Since February has no 31st, it's going to March 3rd. Then you are telling it to go to the end of that month which is why you are getting the end of March after January and not February.

PHP display all dates between a set of dates as list

Suppose I have 2 dates say 29 Aug 2014 and 3 Sep 2014. I need to display all dates between these to dates in the below format.
Aug 2014
29 Fri
30 Sat
31 Sun
Sept 2014
01 Mon
02 Tue
03 Wed
I know how to print all the dates like 29,30,31,1,2,3. But what I am unable to do is to get the month names in between.
Pretty easy question to be honest, pretty basic sollution possible..
$dateRange = new DatePeriod(
new DateTime('2014-07-28'),
new DateInterval('P1D'),
new DateTime('2014-08-04 00:00:01')
);
$month = null;
foreach ($dateRange as $date)
{
$currentMonth = $date->format('m Y');
if ($currentMonth != $month)
{
$month = $date->format('m Y');
echo $date->format('F Y').'<br />';
}
echo $date->format('d D').'<br />';
}
Above sollution results in:
July 2014
28 Mon
29 Tue
30 Wed
31 Thu
August 2014
01 Fri
02 Sat
03 Sun
Do mind it needs PHP >= 5.3 (due to the use of DatePeriod), but the actual logic to solve your question is easy to implement regardless of the used PHP version.
$timeS = strtotime("29 Aug 2014");
$timeE = strtotime("3 Sep 2014");
$monthS = -1;
$time = $timeS;
while ($time < $timeE) {
if ($monthS != date("n", $time)) {
echo date("M Y", $time) . "\n";
$monthS = date("n", $time);
}
echo date("d D", $time) . "\n";
$time = strtotime("+1 day", $time);
}
Edit: After doing it I'm pretty ok with #hindmost comment :)
I think , this is the complete code , as you wanted.
Executed code is here...
http://phpfiddle.org/main/code/3cbe-4855
<?php
$currentMonth = null;
$timeS = strtotime("29 Aug 2013");
$timeE = strtotime("3 Sep 2014");
$time = $timeS;
while ($time < $timeE) {
$month = date("M", $time);
$year = date("Y", $time);
if ($month != $currentMonth)
echo "<br /><h3>".$month."- ".$year."</h3>";
$currentMonth = $month;
echo "<br />".date("d D", $time);
$time = strtotime("+1 day", $time);
}
?>

Categories