PHP subtract months in a year

Hi All,

I am new to PHP and need help with creating PHP code that can just subtract 2 for the current month.  Our current web page has 2 drop down menus; 1 with the month (value is a number), 2 is the year.  so what I would need it to do is:
If month = 8 and year = 2015 then month = 6 and year = 2015

If month = 1 and year = 2015 then month = 11 and year = 2014
thomasm1948Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark BradyPrincipal Data EngineerCommented:
How is the php script getting the information? More than likely it is coming in from the webpage as a $_POST (POST) request so all you need to do (as long as it is a number) is something like this:

<?php

$month = $_POST['month'] - 2;

Open in new window

0
Ray PaseurCommented:
0
thomasm1948Author Commented:
It is getting it from dropdown menus

I was thinking  about something like this but I am not sure

if ($this->ReportR_Month <= 2)
      {
            $this->reportRToYear= $this->ReportR_Year - 1;
            if ($this->ReportR_Month == 1)
            {
                  $this->reportRToMonth = 11;
            }
            if ($this->ReportR_Month == 2)
            {
                  $this->reportRToMonth = 12;
            }
      }
      else{$this->reportRToMonth = $this->ReportR_Month - 2; }
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Ray PaseurCommented:
Please see: http://iconoun.com/demo/temp_thomasm1948.php
<?php // demo/temp_thomasm1948.php

/**
 * http://www.experts-exchange.com/questions/28714640/PHP-subtract-months-in-a-year.html
 * http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html
 * http://php.net/manual/en/function.mktime.php
 */
error_reporting(E_ALL);
echo '<pre>';

// EXAMPLE ONE
$month = 6;
$year  = 2015;

// NORMALIZE THE DATE INFORMATION INTO THE ISO-8601 DATETIME STRING
$iso = date('c', strtotime("$year-$month-01"));
echo PHP_EOL . $iso;

// SUBTRACT TWO MONTHS
$sub = mktime
( 0,0,0
, date('m', strtotime($iso)) -2
, date('d', strtotime($iso))
, date('Y', strtotime($iso))
)
;

// SHOW NEW DATE
$new = date('c', $sub);
echo PHP_EOL . $new;
echo PHP_EOL;

// EXAMPLE TWO
$month = 1;
$year  = 2015;

// NORMALIZE THE DATE INFORMATION INTO THE ISO-8601 DATETIME STRING
$iso = date('c', strtotime("$year-$month-01"));
echo PHP_EOL . $iso;

// SUBTRACT TWO MONTHS
$sub = mktime
( 0,0,0
, date('m', strtotime($iso)) -2
, date('d', strtotime($iso))
, date('Y', strtotime($iso))
)
;

// SHOW NEW DATE
$new = date('c', $sub);
echo PHP_EOL . $new;

Open in new window

0
thomasm1948Author Commented:
How would I have it assign 1 variable with the month and another with the year?  reason why I need it split is to pass it to a SQL query

thank you for all of your help in advance
0
Julian HansenCommented:
Take a look at the DateTime class specifically the sub() function
http://php.net/manual/en/datetime.sub.php

You will also want to look at the DateInterval class
http://php.net/manual/en/dateinterval.construct.php

Sample code to subtract 2 months

<?php
$x = new DateTime('2015-01-21');
$x->sub(new DateInterval('P2M'));
print_r($x);

Open in new window

0
Ray PaseurCommented:
Here's a slightly different take on it - using a form for input.
http://iconoun.com/demo/temp_thomasm1948.php?month=2&year=2013
<?php // demo/temp_thomasm1948.php

/**
 * http://www.experts-exchange.com/questions/28714640/PHP-subtract-months-in-a-year.html
 * http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html
 * http://php.net/manual/en/function.mktime.php
 */
error_reporting(E_ALL);
echo '<pre>';

// IF THERE IS A REQUEST, PROCESS IT TO SUBTRACT TWO MONTHS
$m = !empty($_GET['month']) ? (int)$_GET['month'] : 0;
$y = !empty($_GET['year'])  ? (int)$_GET['year']  : 0;
if ($m)
{
    $old = date('Y-m-d', strtotime("$y-$m-01"));
    $mkt = mktime
    ( 0,0,0
    , date('m', strtotime($old)) -2
    , date('d', strtotime($old))
    , date('Y', strtotime($old))
    );
    $new = date('Y-m-d', $mkt);

    echo PHP_EOL . "Old: $old";
    echo PHP_EOL . "New: $new (Old - 2 Months)";
}

// THE MONTHS
$months =
[  1 => 'Jan'
,  2 => 'Feb'
,  3 => 'Mar'
,  4 => 'Apr'
,  5 => 'May'
,  6 => 'Jun'
,  7 => 'Jul'
,  8 => 'Aug'
,  9 => 'Sep'
, 10 => 'Oct'
, 11 => 'Nov'
, 12 => 'Dec'
];

// THE YEARS
$years = range(2013, 2016);

// PREPARE THE MONTH SELECTIONS
$months_html = '<select name="month">' . PHP_EOL;
$months_html .= '<option value="0">Select Month</option>' . PHP_EOL;
foreach ($months as $n => $m)
{
    $months_html .= '<option value="' . $n . '">' . $m . '</option>' . PHP_EOL;
}
$months_html .= '</select>' . PHP_EOL;

// PREPARE THE YEAR SELECTIONS
$years_html = '<select name="year">' . PHP_EOL;
$years_html .= '<option value="0">Select Year</option>' . PHP_EOL;
foreach ($years as $n)
{
    $years_html .= '<option value="' . $n . '">' . $n . '</option>' . PHP_EOL;
}
$years_html .= '</select>' . PHP_EOL;

// PREPARE THE FORM
$form = <<<EOD
<form>
$months_html
$years_html
<input type="submit" />
</form>
EOD;

echo $form;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
... I need it split to pass it to a SQL query
A database table that has month and year in different columns is a code smell.  SQL has the DATETIME data type, which is almost always the right way to represent date and time values in a database.  I think if you follow the background and examples in the date / time article, you'll get up to speed on this stuff pretty quickly.

If you're new to PHP and want to find some good learning resources, this article can help.  Just skip over the basic computer science stuff that you already know.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Best of luck with your project, ~Ray
0
thomasm1948Author Commented:
This idea worked out perfect.  The only thing that I added was:

list($year1,$month2,$day2)=explode("-", $new);

Thank you for all of your help
0
Ray PaseurCommented:
Thanks for the points and best of luck with your project, Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.