Solved

php year month loop function

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

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 108

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Read about why website design really matters in today's demanding market.
This video teaches users how to migrate an existing Wordpress website to a new domain.
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now