Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php year month loop function

Posted on 2012-12-30
3
Medium Priority
?
2,187 Views
Last Modified: 2012-12-30
$curYear = date('Y');
          for ($k = $curYear; $k >= 2011; $k--)

          for ($i = 12; $i >= 1; $i--)

$monthName = date("F", mktime(0, 0, 0, $i, 10));

Open in new window


what i want to modify in this code is

like if year = 2012 i will start from 2012 and the current month ...and decrement the month till January..



and if year less than curr year like 2011 fetch all month..


let say we are in year 2013 so the output is
2013 from month 12 to 1
and 2012 from month 12 to 1
and 2011 from month 12 to 1

i want to modify it to be like
2013 from current  month  to 1
and 2012 from month 12 to 1
and 2011 from month 12 to 1
0
Comment
Question by:afifosh
  • 2
3 Comments
 
LVL 13

Expert Comment

by:darren-w-
ID: 38730967
well to get months this year to today:

<?php 
$thisYear = range(1,date("n"));
foreach ($thisYear as $month){
print date("F",mktime(0,0,0,$month,10));
}

//previous years :

$thisYear = range(1,12); //then just add years

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38730986
Please read the article here.  It will tell you everything you need to know about the basics of handling PHP and SQL date/time values.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 38730996
Here is how I might do it.
<?php // RAY_temp_afifosh.php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
date_default_timezone_set('America/Chicago');
echo '<pre>';

/* PROBLEM DEFINITION
i want to modify it to be like
2013 from current  month  to 1
and 2012 from month 12 to 1
and 2011 from month 12 to 1
*/

// A DATE IN 2013
$start = '2013-04-15';

// A DATE TO STOP GENERATING DATA
$stop  = '2011-01-01';

// A LOOP TO SHOW THE DATES
while ($start >= $stop)
{
    $start = date('Y-m-01', strtotime($start));
    echo PHP_EOL . $start;
    $start = date('Y-m-01', strtotime($start . ' - 1 MONTH'));
}

Open in new window

Best to all, ~Ray
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question