hello all!

i need to add a number (months) to a date from a mysql table in the format of YYYY-MM-DD . the problem i am having is if the month goes over a year, incrementing the year.

any suggestions on how i could accomplish this. i tried converting the date with strtotime and adding $contract but that just adds to the end of the string incremementing the minutes (ithink).

// check for contract closing

$query = "SELECT * FROM products_purchased";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);

for ($i=0; $i<$num_results; $i++) {
	$row = mysql_fetch_array($result);

// need to add contract (months to date purchased

$date_purchased = $row['date_purchased'];
$contract = $row['contract'];
}

thanks 😃

    How about trying this.

    $query = "SELECT *,DATE_ADD( date_purchased , INTERVAL contract month ) as contract_end FROM products_purchased";

    HalfaBee

      $datestamp = "2003-10-15";
      $datestamp = explode("-", $datestamp);
      $year = $datestamp[0];
      $month = $datestamp[1];
      $day = $datestamp[2];
      
      // add two months
      $month = $month + 2;
      if ($month > "12")
          {
          $month = $month - 12;
          $year++;
          }
      
      $updated_datestamp = $year . "-" . $month . "-" . $day;

      -=Lazzerous=-

        Or in one line.

        $updated_datestamp = date( "Y-m-d" , strtotime(     "$date_purchased  +$contract  month" ) );
        

        HalfaBee

          wicked!

          thanks halfabee..that seems to be exactly what im looking for

          😃

            Replace $month = $month + $contract;

            if ($month > 12) {
                $month = $month - 12;
                $year++;
            }

            with this

            $month = (($month + $contract-1)%12)+1;

            HalfaBee

              hey HalfaBee..sorry to bug you again..

              Replace $month = $month + $contract;

              if ($month > 12) {
              $month = $month - 12;
              $year++;
              }

              with this

              $month = (($month + $contract-1)%12)+1;

              can i not achieve the same thing as above with the code you supplied?

              $updated_datestamp = date("Y-m-d" , strtotime( "$date_purchased + $contract month" ));

              thanks mate

                Yes, a bit of cross posting was going on.

                The sql answer should have worked as well.

                HalfaBee

                  Write a Reply...