如何从任何日期减去1个月并从数据库中获取该月的值

时间:2017-05-29 10:01:03

标签: php codeigniter

$ payment_date是我想减去1个月的日期。 我该怎么办?

<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是我想减去1个月的日期。 我该怎么办?我只想从上个月付款

2 个答案:

答案 0 :(得分:1)

容易!

<?php

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

在此处查看https://3v4l.org/UWaGY

您还可以将日期字符串作为构造函数提供。 new DateTime('2014-09-18');

答案 1 :(得分:1)

@LEARNER尝试这样,在这里检查输出和代码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;
?>