PHPでカレンダーを作成中なのですが、以下のようにすると、画像のようになります。
<?php $body =''; $period = new DatePeriod( new DateTime('first day of this month'), new DateInterval('P1D'), new DateTime('first day of next month') ); foreach ($period as $day) { $body .=sprintf('<td>%d</td>',$day->format('d')); } ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8" /> <title>Calendar</title> <link rel="stylesheet" hrel="styles.css"> <!--[if IE]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link rel="stylesheet" href="styles.css"> </head> <body> <table> <thead> <tr> <th><a href="">«</a></th> <th colspan="5">2018</th> <th><a href="">»</a></th> </tr> </thead> <tbody> <tr> <td>Sun</td> <td>Mon</td> <td>Tue</td> <td>Wed</td> <td>Thu</td> <td>Fri</td> <td>Sat</td> </tr> <tr> <?php echo $body;?> </tr> </tbody> <tfoot> <tr> <th colspan="7"><a href="">today</a></th> </tr> </tfoot> </table> </body> </html>
この出力は期待通りなのですが、コードを以下のように変更すると、
<?php $body ='あ'; $period = new DatePeriod( new DateTime('first day of this month'), new DateInterval('P1D'), new DateTime('first day of next month') ); foreach ($period as $day) { $body .=sprintf('<td>%d</td>',$day->format('d')); } ?> (以下同じ)
このようになります。
しかし、なぜでしょうか?
$body .=sprintf('<td>%d</td>',$day->format('d'));
このコードで
$bodyと日付が結合されているので、
あ1あ2あ3・・・・・と続いていくとおもうのですが。。。。
理由がわかる方がいれば教えてください。
回答1件
あなたの回答
tips
プレビュー