Reputation: 533
I have created a loop which will display date 2004 to 2014 in a formatted way. But the problem is, it is showing 204 instead of 2004 and continue this till 209.. So, how to show those year in formatted way, like 2004,2005,2006 etc.
Here is the code i have created, tell me where to fix:
<?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++;
}
?>
Upvotes: 2
Views: 266
Reputation: 375
According to your code, you can try this. Though its not a standard way:
<?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++;
}
?>
Upvotes: 1
Reputation: 403
You could do it also with a (Quite) different Structure:
<?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';
}
?>
Upvotes: 2
Reputation: 1585
You need to use the str_pad
function (manual). In your case, it goes like this:
<?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++;
}
?>
Upvotes: 0
Reputation: 1965
Use str_pad:
echo $x.' Jan 20'.str_pad($yr, 2, '0', STR_PAD_LEFT).'<br>';
Is more appropriate to use the function cal_days_in_month of the variable $x:
<?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++;
}
?>
Upvotes: 0
Reputation: 95131
All you need is one loop
$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");
}
Upvotes: 6
Reputation: 2733
I am not sure why are you doing this by so many loops, use this instead of $yr and you will get the correct year printed:
str_pad($yr, 2, '0', STR_PAD_LEFT);
Best
Upvotes: 3
Reputation: 39540
Why go through such a long and odd process, when you can do something like this?
<?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);
}
?>
Output:
01 Jan 2004
02 Jan 2004
03 Jan 2004
...
29 Dec 2012
30 Dec 2012
31 Dec 2012
This also has the added bonus of not showing "31 Feb 2008" etc., as that date doesn't even exist.
Codepad example of the code (WARNING: long output!)
Upvotes: 5
Reputation: 403
It's because you're setting $yr
like that:
$yr = 4;
Try this:
$yr = sprintf('%02d', $yr);
echo "$x Jan 20$yr<br>";
Upvotes: 0
Reputation: 421
This is not standard, but you can add an if loop inside that while loop like this:
while($x <= 31)
{
if ($yr>=10)
{
echo "$x Oct 20$yr<br>";
}
else
{
echo "$x Oct 200$yr<br>";
}
$x++;
}
While you'd wanna do something like this beats me though
Upvotes: 0
Reputation: 1436
Easiest fix is to set $yr = 2004 and loop while $yr < 2014. You are not padding your numbers with a leading zero, hence 204, 205, etc.
Upvotes: 2