我创建了一个循环,它将以格式化的方式显示2004年到2014年的日期。但问题是,它显示的是204而不是2004年,并持续到209.那么,如何以格式化的方式显示那些年份,如2004,2005,2006等。
这是我创建的代码,告诉我在哪里修复:
<?php
$yr = 4;
while ($yr <= 14) {
$x = 1;
while ($x <= 31) {
echo "$x Jan 20$yr<br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Feb 20$yr<br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Mar 20$yr<br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Apr 20$yr<br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x May 20$yr<br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Jun 20$yr<br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Jul 20$yr<br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Aug 20$yr<br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Sep 20$yr<br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Oct 20$yr<br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Nov 20$yr<br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Dec 20$yr<br>";
$x++;
}
$yr++;
}
?>
答案 0 :(得分:6)
你需要的只是一个循环
$start = 2004;
$end = 2014;
$dateTime = new DateTime();
$dateTime->setDate($start, 1, 1);
echo "<pre>";
while ( $dateTime->format("Y") <= $end ) {
echo $dateTime->format("d M Y"), PHP_EOL;
$dateTime->modify("+1 day");
}
答案 1 :(得分:5)
为什么要经历这样一个漫长而奇怪的过程,当你可以做这样的事情时?
<?php
$yearStart = 2004;
$yearEnd = 2012;
$unixTime = strtotime($yearStart . "-01-01 00:00:00");
$endUnixTime = strtotime($yearEnd . "-12-31 23:59:59");
while ($unixTime < $endUnixTime) {
echo date("d M Y", $unixTime) . PHP_EOL;
$unixTime = strtotime("+1 day", $unixTime);
}
?>
输出:
01 Jan 2004
02 Jan 2004
03 Jan 2004
...
29 Dec 2012
30 Dec 2012
31 Dec 2012
这也有额外的好处,即没有显示“2008年2月31日”等,因为该日期甚至不存在。
答案 2 :(得分:3)
我不确定你为什么要通过这么多循环来做这个,用这个而不是$ yr你会得到正确的年份:
str_pad($yr, 2, '0', STR_PAD_LEFT);
最佳
答案 3 :(得分:2)
最简单的解决方法是设置$ yr = 2004并循环,而$ yr&lt;你不是用前导零填充你的数字,因此是204,205等。
答案 4 :(得分:2)
你也可以使用(相当)不同的结构:
<?php
function displayDate($yr, $yrMax) {
if ($yr > $yrMax) {
return true;
}
else {
displayMonth($yr);
$yr++;
return displayDate($yr, $yrMax);
}
}
function displayMonth($yr, $month = 1) {
if ($month > 12) {
return true;
}
else {
displayDay($yr, $month);
return displayMonth($yr, $month+1);
}
}
function displayDay($yr, $month, $day = 1, $dayMax = 31) {
if ($day > $dayMax) {
return true;
} else {
$displayMonth = getMonth($month);
echo "$day $displayMonth $yr<br>";
$day++;
return displayDay($yr, $month, $day, $dayMax);
}
}
function getMonth($month) {
switch($month){
case 1:
return 'Jan';
case 2:
return 'Feb';
case 3:
return 'Mar';
case 4:
return 'Apr';
case 5:
return 'May';
case 6:
return 'Jun';
case 7:
return 'Jul';
case 8:
return 'Aug';
case 9:
return 'Sep';
case 10:
return 'Oct';
case 11:
return 'Nov';
case 12:
return 'Dec';
}
}
//Here we call the structure build above.
if (displayDate(2004, 2014)) {
echo 'Done';
}
?>
答案 5 :(得分:1)
根据您的代码,您可以试试这个。虽然它不是标准方式:
<?php
$yar = 4;
while ($yar <= 9) {
$ax = 1;
while ($ax <= 31) {
echo "$ax Jan 200$yar <br>";
$ax++;
}
$ax = 1;
while ($ax <= 31) {
echo "$ax Feb 200$yar <br>";
$ax++;
}
$ax = 1;
while ($ax <= 31) {
echo "$ax Mar 200$yar <br>";
$ax++;
}
$ax = 1;
while ($ax <= 31) {
echo "$ax Apr 200$yar <br>";
$ax++;
}
$ax = 1;
while ($ax <= 31) {
echo "$ax May 200$yar <br>";
$ax++;
}
$ax = 1;
while ($ax <= 31) {
echo "$ax Jun 200$yar <br>";
$ax++;
}
$ax = 1;
while ($ax <= 31) {
echo "$ax Jul 200$yar <br>";
$ax++;
}
$ax = 1;
while ($ax <= 31) {
echo "$ax Aug 200$yar <br>";
$ax++;
}
$ax = 1;
while ($ax <= 31) {
echo "$ax Sep 200$yar <br>";
$ax++;
}
$ax = 1;
while ($ax <= 31) {
echo "$ax Oct 200$yar <br>";
$ax++;
}
$ax = 1;
while ($ax <= 31) {
echo "$ax Nov 200$yar <br>";
$ax++;
}
$ax = 1;
while ($ax <= 31) {
echo "$ax Dec 200$yar <br>";
$ax++;
}
$yar++;
}
$yr = 10;
while ($yr <= 14) {
$x = 1;
while ($x <= 31) {
echo "$x Jan 20$yr <br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Feb 20$yr <br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Mar 20$yr <br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Apr 20$yr <br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x May 20$yr <br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Jun 20$yr <br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Jul 20$yr <br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Aug 20$yr <br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Sep 20$yr <br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Oct 20$yr <br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Nov 20$yr <br>";
$x++;
}
$x = 1;
while ($x <= 31) {
echo "$x Dec 20$yr <br>";
$x++;
}
$yr++;
}
?>
答案 6 :(得分:0)
这不是标准的,但你可以像这样在while循环中添加一个if循环:
while($x <= 31)
{
if ($yr>=10)
{
echo "$x Oct 20$yr<br>";
}
else
{
echo "$x Oct 200$yr<br>";
}
$x++;
}
虽然你想做这样的事情但是打败了我
答案 7 :(得分:0)
这是因为你正在设置$yr
:
$yr = 4;
试试这个:
$yr = sprintf('%02d', $yr);
echo "$x Jan 20$yr<br>";
答案 8 :(得分:0)
您需要使用str_pad
函数(manual)。在你的情况下,它是这样的:
<?php
$yr = 4;
while ($yr <= 14) {
$x = 1;
while ($x <= 31) {
echo "$x Jan 20".str_pad($yr, 2, "0",STR_PAD_LEFT)."<br>";
$x++;
}
$yr++;
}
?>
答案 9 :(得分:0)
使用str_pad:
echo $x.' Jan 20'.str_pad($yr, 2, '0', STR_PAD_LEFT).'<br>';
更适合使用变量 $ x 的函数cal_days_in_month:
<?php
$yr = 4;
while ($yr <= 14) {
$year = '20'.str_pad($yr, 2, '0', STR_PAD_LEFT);
for($month = 1; $month <= 12; $month++) {
//number of days this month
$daysCount = cal_days_in_month(CAL_GREGORIAN, $month, $year);
//catches the month spelled
$timestamp = mktime(0, 0, 0, $month, 1, $year);
$monthText = date('M', $timestamp);
for($day = 1; $day <= $daysCount; $day++) {
echo $day.' '.$monthText.' '.$year.'<br>';
}
}
$yr++;
}
?>