Solved

php year month loop function

Posted on 2012-12-30
3
2,013 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
I am having a  Git   issue 6 42
How easy is it to support multiple logins to my website on one web browser session? 5 36
PHP Echo with auto submit 8 30
Ajax and PHP 9 30
This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

830 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