Solved

php year month loop function

Posted on 2012-12-30
3
1,965 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 109

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 109

Accepted Solution

by:
Ray Paseur earned 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

803 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