LEARNER
LEARNER

Reputation: 81

how to subtract 1 month from any date and fetch value of that month from database

$payment_date is date from which I want to subtract 1 month. how could I do ?

<table class="table table-bordered table-hover" id="dataTables-example">
   <thead>
      <tr>
         <th>Payment Month</th>
         <th>Payment Date</th>
         <th>Gross Salary </th>
         <th>Total Deduction</th>
         <th>Net Salary</th>
         <th>Advance Salary</th>
         <th>Incentive</th>
         <th>Fine Deduction</th>
         <th>Payment Amount</th>
         <th class="hidden-print">Details</th>
      </tr>
   </thead>
   <tbody>
      <?php
         if (!empty($payment_history)): foreach ($payment_history as $v_payment_history) :
                 ?>
      <tr>
         <td><?php echo date('F-Y', strtotime($v_payment_history->payment_for_month)); ?></td>
         <td><?php echo date('d-M-y', strtotime($v_payment_history->payment_date)); ?></td>
         <td><?php echo $gross = $v_payment_history->basic_salary + $v_payment_history->house_rent_allowance + $v_payment_history->medical_allowance + $v_payment_history->special_allowance + $v_payment_history->fuel_allowance + $v_payment_history->phone_bill_allowance + $v_payment_history->other_allowance; ?></td>
         <td><?php echo $deduction = $v_payment_history->tax_deduction + $v_payment_history->provident_fund + $v_payment_history->other_deduction; ?></td>
         <td><?php echo $net_salary = $gross - $deduction; ?></td>
         <td><?php echo $v_payment_history->award_amount; ?></td>
         <td><?php echo $v_payment_history->incentive; ?></td>
         <td><?php echo $v_payment_history->fine_deduction; ?></td>
         <td><?php echo $v_payment_history->payment_amount; ?></td>
         <td class="hidden-print"><?php echo btn_view('admin/payroll/salary_payment_details/' . $v_payment_history->salary_payment_id) ?></td>
      </tr>
      <?php
         endforeach;
         ?>
      <?php else : ?>
      <tr>
         <td colspan="9">
            <strong>There is no data for display</strong>
         </td>
      </tr>
      <?php endif; ?>
   </tbody>
</table>

$payment_date is date from which I want to subtract 1 month. how could I do ? I want payment only of last month from the month I'm giving payment

Upvotes: 1

Views: 509

Answers (2)

lazyCoder
lazyCoder

Reputation: 2561

@LEARNER try it like this, check the output and code online here https://eval.in/806939:

<?php
//suppose that date is 2017/05/29
$yourDate = strtotime('2017/05/29 -1 month'); // substract 1 month from that date and converting it into timestamp
$desiredDate = date("Y-m-d", $yourDate);
echo $desiredDate;
?>

Upvotes: 1

delboy1978uk
delboy1978uk

Reputation: 12382

Easy!

<?php

$date = new DateTime();
$date->modify('-1 month');
echo $date->format('Y-m-d H:i:s');

See it here https://3v4l.org/UWaGY

You can also feed in a date string as a constructor. new DateTime('2014-09-18');

Upvotes: 1

Related Questions