why is for loop true on equal - php

My for loop is true when variables are equal.
Why is that when i'm only using "smaller than"?
I have a while loop that prints dates from mysql.
$today_nr is a day nr stored in db. ex: 02
Now i´d like a for loop to print out dates that is missing.
ex: 01, 02, 03. If first reccord in mysql is 04.
So i use this for loop. But it will echo both "01" from mysql($today_nr) and "1" from $i
PHP
$today_nr = $date->format('d');
$i = 1;
for(;$i < $today_nr; $i++){
echo $i;
}
FULL SCRIPT
https://eval.in/367692
Line 532
The Print for loop is printing 1 after while loop printed 01.
01 Ons 04:41 13:10 0.50 7.97 -0.03 1.32
1
02 Tor 04:40 13:18 0.50 8.13 0.13 1.33
2
3
4
5
6
07 Tis 04:41 12:58 0.50 7.77 -0.23 1.32
7
08 Ons 04:43 13:08 0.50 7.92 -0.08 1.28

It isn't like what you are saying in my case the code below works perfect with not an issue as you provided and
It works as you expects:
<?php
$today_date = date('d', time());
// $today_nr = $date->format('d');
$i = 1;
for(;$i < $today_date; $i++){
echo $i; // result: 12345678910111213141516171819
}

I solved it like this:
PHP
//Loop out dates missing in db
//If $i is bigger than today nr
if($i > $today_nr){
$i = $today_nr;
}
//Loop out missing days
for(;$today_nr != $i;){
//Set data about day
$iDate = $year."-".$selected_month."-".$i;
$iDate = new DateTime($iDate);
$iDay = $iDate->format('D');
//Translate iDay. Eng - Swe
include('../../../include/translate_days_iDay.php');
//Echo out table
echo "<tr>";
echo "<td class='print_table_text'>".$iDate->format('W')."</td>";
echo "<td class='print_table_text'>".$iDate->format('d')." ".$iDay."</td>";
echo "</tr>";
//Add to i
$i++;
//if i is bigger than a month
if($i > $days_in_month){
break;
}
}
//If today and i is equal
if($today_nr == $i){
$i++;
}

Related

php foreach print <br> tag if different date

I hope I can be clear enough.
I'm retrieving dates from database (day only). I have a loop and I want to print something like that:
1 april
1 april
2 april
3 april
3 april
3 april
etc...
So : same days = no <br> tag, different days print a <br>.
My actual code:
$prec;
foreach ($list as $rs) {
$day = date("d", strtotime($rs['date']))." ";
if ($day === $prec) {
$space = "";
} else {
$space ="<br>";
}
$prec = $day;
echo $day;
echo date("F", strtotime($rs['date'])).", ";
echo $space;
}
But this output is wrong:
23 March
25 March
27 March
27 March
27 March
27 March
27 March
28 March
28 March
28 March
02 April
03 April
04 April
What am I missing?
The problem is the placement of where you output the line break - if you output the line break before the date rather than after, the output will be as intended.
echo $space;
echo $day;
echo date("F", strtotime($rs['date'])).", ";
That is because your comparison is based on how the current value compares to the previous one, so the generated result needs to be placed between these two days to follow the same logic
Keep in mind that your code will still return wrong output if you happen to have the same day in a separate month next to each other, because you only compare by day (if you compare by the full date, it will work better)
you have to echo $space before echo $day
//same days = no <br> tag, different days print a <br>
$list = [1, 3, 3, 3,4 ];
$prec ='';
foreach ($list as $day) {
if ($day === $prec) {
$space = ",";
} else {
$space ="<br>";
}
echo $space;
echo $day;
$prec = $day;
}
Result :
<br>1<br>3,3,3<br>4

Hide links after the current day

I'm currently building Advent calendar, and as you know, only 24 days are shown. Each of the day has a link that will shown its page.
I'm trying to show links only to the pages from day 1 to current day, not after current day.
For example, If today is 20 December, the rest (21, 22, 23, 24 December should have no link, no a href)
Here's how my code looks right now:
$date = date('Y-m-d');
echo 'Current Date: '.$date.'<br>';
for ($x = 1; $x <= 24; $x++) {
echo "<a class='grid-item' href=".'/region.php?region=Region&tag=2016-12-'.str_pad($x, 2, "0", STR_PAD_LEFT)."><em>$x</em></a><br>";
}
The output right now:
Current Date: 2016-11-11
1
2
3
4
5
..
..
24
Any suggestion would be appreciated.
Just compare $x to the day of the month. If it is greater than it, don't show the link.
$today = new DateTime();
$day = $today->format('j');
echo 'Current Date: '.$today->format('Y-m-d').'<br>';
for ($x = 1; $x <= 24; $x++) {
if ($x <= $day) {
echo "<a class='grid-item' href=".'/region.php?region=Region&tag=2016-12-'.str_pad($x, 2, "0", STR_PAD_LEFT)."><em>$x</em></a><br>";
}
else {
echo $x;
}
}

Loop the days of the month throughout the whole month

I am building multi-calendar, I have a horizontal looking interface:
I am trying to run the days of the week S,M,T,W,T,F,S
throughout the whole month instead of just the first 7 as in the picture.
the function which draw the calendar:
//our case "SUN"
if(AC_START_DAY=="sun"){
for($k=0; $k<7; $k++){
$weekday = mb_substr($lang["day_".$k.""],0,1,'UTF-8');
$list_day_titles.='<li class="cal_weekday"> '.$weekday.'</li>';
}
}
//If we chose Monday as start week.
else{
if ($first_week_day == 0) $first_week_day =7;
for($k=1; $k<=7; $k++){
if($k==7) $weekday = mb_substr($lang["day_0"][0],0,1,'UTF-8');
else $weekday = mb_substr($lang["day_".$k.""],0,1,'UTF-8');
$list_day_titles.='<li title="'.$lang["day_".$k.""].'"> '.$weekday.'</li>';
}
}
The lang file:
$lang["day_0"] = "Sunday";
$lang["day_1"] = "Monday";
$lang["day_2"] = "Tuesday";
$lang["day_3"] = "Wednesday";
$lang["day_4"] = "Thursday";
$lang["day_5"] = "Friday";
$lang["day_6"] = "Saturday";
Already defined
$month=sprintf("%02s",$month);
// define vars
$today_timestamp = mktime(0,0,0,date('m'),date('d'),date('Y')); # current timestamp - used to check if date is in past
$this_month = getDate(mktime(0, 0, 0, $month, 1, $year)); # convert month to timestamp
$first_week_day = $this_month["wday"]; # define first weekday (0-6)
$days_in_this_month = cal_days_in_month(CAL_GREGORIAN,$month,$year); # define number of days in week
$day_counter_tot = 0; # count total number of days showin INCLUDING previous and next months - use to get 6th row of dates
Looks like the $lang["day_".$k.""] is just counting the days from 0 to 6.. how can i make is loop untill the end of the month?
NOTE: I tried increasing the $k<7 just more empty blue boxes appear.
Use the loop to the 30/31 day.
And then change this line
$weekday = mb_substr($lang["day_".$k.""],0,1,'UTF-8');
to
$weekday = mb_substr($lang["day_".$k%7.""],0,1,'UTF-8');
This should give you the day 0 for every sunday.
0 % 7 = 0 (sunday)
1 % 7 = 1 (monday)
...
7 % 7 = 0 (sunday again)
8 % 7 = 1 (monday again)
You can use this code to generate all the days of the current month.
for ($date = strtotime(date('Y-m-01')); $date < strtotime(date('Y-m-t')); $date = strtotime("+1 day", $date)) {
echo date("l-d", $date)."<br>";
}
Will print all the days of the current month as follows.
Thursday-01
Friday-02
Saturday-03
Sunday-04
Monday-05
Tuesday-06
Wednesday-07
Thursday-08
Friday-09
Saturday-10
Sunday-11
Monday-12
Tuesday-13
Wednesday-14
Thursday-15
Friday-16
Saturday-17
Sunday-18
Monday-19
Tuesday-20
Wednesday-21
Thursday-22
Friday-23
Saturday-24
Sunday-25
Monday-26
Tuesday-27
Wednesday-28
Thursday-29
Friday-30
Looks like you almost got it right.
You only need to slightly modify your code to make it work the way you want it to.
You should just change your code to:
$number_of_days_in_the_future = 42; // Here you can put in the number of days for which you want to display the corresponding letter, and based on your screenshot that is 42
//our case "SUN"
if(AC_START_DAY=="sun"){
for($i=0; $i<$number_of_days_in_the_future; $i++){
$k = $i % 7;
$weekday = mb_substr($lang["day_".$k.""],0,1,'UTF-8');
$list_day_titles.='<li class="cal_weekday"> '.$weekday.'</li>';
}
}
//If we chose Monday as start week.
else{
if ($first_week_day == 0) $first_week_day =7;
for($i=1; $i<=$number_of_days_in_the_future; $i++){
$k = $i % 7;
if($k==7) $weekday = mb_substr($lang["day_0"][0],0,1,'UTF-8');
else $weekday = mb_substr($lang["day_".$k.""],0,1,'UTF-8');
$list_day_titles.='<li title="'.$lang["day_".$k.""].'"> '.$weekday.'</li>';
}
}
Please note that I only tried to fix your code so it works as you expect it to.
There's probably a more elegant solution but I don't really know your full code so I would just be guessing if I tried to offer you another approach.
I hope this will help you.
Cheers

php foreach next month dates

What I need is get listed next month days, every new day on new line.
Actually I want it look next:
date | username1 | username2
_________|___________|__________
| |
Mon - 01 | user5 | user2
| |
Tue - 02 | user3 | user2
....
and so on
Later i will be collect these dates and usernames to and update sql table (same type, like example here).
I have this code:
$workdays = array();
$type = CAL_GREGORIAN;
$month = date('n'); // Month ID, 1 through to 12.
$year = date('Y'); // Year in 4 digit 2009 format.
$day_count = cal_days_in_month($type, $month, $year); // Get the amount of days
//loop through all days
for ($i = 1; $i <= $day_count; $i++) {
$date = $year.'-'.$month.'-'.$i; //format date
$workdays[] = $i;
and with foreach echo i get days 1, 2, 3 ... and so on correctly, everyone on new line. But if i place inside foreach this:
<?php foreach($workdays as $value['date']): ?>
<tr>
<td><?php if ($value['date'] != 'dd'){
setlocale(LC_TIME, 'fi_FI.UTF-8');
echo ucfirst(strftime("%a - %d", strtotime($value['date'])));
} else {
echo 'wrong';
}?></td>
<td> </td>
</tr>
<?php endforeach ?>
I get on every new line Mon - 01.
What is wrong (after that, that i newbie on php)?
Try something like this.
<?php
$day_count = date('t',strtotime('+1 month'));
$month = date('m');
$year = date('Y');
if ($month == 12) { $month = 1; $year++; }
for($i = 1; $i<=$day_count; $i++) {
echo '<tr>';
echo '<td>'.date('D',strtotime("$month/$i/$year")).' '.$i."</td>";
echo '<td> </td>';
echo '</tr>';
}

Auto increment years in loop every year [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to increment date with 1 (day/year) in PHP?
Im not really sure where to begin with this but im trying to make a year start at 1928 and stop at 1948 and for every year the years increment by one so since its 2012 the date ranges are 1928 - 1948 and for 2013 it would be 1929 - 1949 and 2014 would be 1930 - 1950 and so on...
right now i just have a basic loop for when to start and stop the years but its not too dynamic, like i said im pretty much at a blank on where to begin other then date('Y')+1.
for($i=1928;$i<=date('Y');$i++)
{
echo '<option value='.$i.'>'.$i.'</option>';
if($i == '1948'){break;}
}
So you want it to go between current year minus 84 and the current year minus 64? Use this code:
$firstYear = (int)date('Y') - 84;
$lastYear = $firstYear + 20;
for($i=$firstYear;$i<=$lastYear;$i++)
{
echo '<option value='.$i.'>'.$i.'</option>';
}
Edit: updated for performance. Current year is determined before the loop (per Pitchinnate's comment).
Try this:
$year = date('Y');
$add = $year - 2012;
$min = 1928 + $add;
$max = $min + 20;
for($i=$min;$i<=$max;$i++)
{
echo '<option value='.$i.'>'.$i.'</option>';
}
I isn't a good idea to have date('Y') or any evaluations done on the for loop as it gets calculated every time through the loop. Article about this.
Something like this?
$base_year = 2012;
$start_year = $base_year - 84;
$end_year = $start_year + 20;
for( $i = $start_year; $i <= $end_year; $i++)
{
echo '<option value='.$i.'>'.$i.'</option>';
}
for($i =0; $i <= 20 ;$i++)
{
$year = date('Y') - 84 + $i;
echo '<option value='.$year.'>'.$year.'</option>';
}
It looks like you have a constant with how far back you want the date range to be from the current year that you can use to write a loop that will work for you.
Since it's 2012 now, and you want the range to start at 1928 when it's 2012, then we can use 2012 - 1928 = 84, so the year that starts the range should always be 84 less than the current year.
Therefore we could write code like:
$startingYear = date('Y') - 84;
$endingYear = $startingYear + 20;
for ($i = $startingYear;$i <= $endingYear;$i++)
{
echo '<option value='.$i.'>'.$i.'</option>';
}

Categories