[PHP] Output 365 days + days of the week
Sep 8, 2020
PHP
timezone
for statement
for beginners
To output today's date, write as follows.
<? php
print (date ('n / j (D)'));
?>
n
gets the month
j
gets the date
D
gets the day of the week in English (first 3 letters)
If you have not set the timezone, you can change it by writing it directly.
<? php
date_default_timezone_set ('Asia / Tokyo');
print (date ('n / j (D)'));
?>
Here, consider how to get the date + day of the week one day later.
<? php
print (date ('n / j (D)', strtotime ('+ 1day'));
?>
With this, I was able to get the date + day of the week one day later.
Strtotime
is an abbreviation forString to Timestamp
A function that can convert a character string to a Timestamp type.
1 day later if + 1day
1 day before -1day
+ 365day
will be one year later
Can be.
strtotime ('+ 1day')
It seems good to repeat this part 365 times using the for statement.
<? php
for ($ i = 1; $ i <= 365; $ i ++); {
$ date = strtotime ('+'. $ I.'day');
print (date ('n / j (D)', $ date));
print "\ n";
}
?>
With this, I got 365 days worth.
[Commentary]
(1) Set variable i = 1
, repeat with 365 or less
, and add 1 to variable i
every time.
for ($ i = 1; $ i <= 365; $ i ++);
(2) Decompose and assign the contents of strtotime
to the variable $ date
$ date = strtotime ('+'. $ I.'day');
// The part 1 of'+ 1day'of strtotime ('+ 1day') is $ i
③ Put the variable $ date
in the part where the strtotime
function was written.
print (date ('n / j (D)', $ date));
④ Line breaks for easy viewing
print "\ n";
By the way, the {} part can also be rewritten as follows
<? php
for ($ i = 1; $ i <= 365; $ i ++):
$ date = strtotime ('+'. $ I.'day');
print (date ('n / j (D)', $ date));
print "\ n";
endfor;
?>
It’s easier to understand what the closing tag is for. The while statement is the same and can be described as follows.
while (....):
....
endwhile;
that’s all. Thank you for your hard work.