Jump to content

How to echo a date value as date + 1 month.


Go to solution Solved by wezhind,

Recommended Posts

I am looking for a way to have my 'start_date', echo ('start_date' + 1 month).  I am looking at the line that reads .... date("F Y",strtotime($row['start_date'])).  Does anyone have a suggestion.  The date format read, July 2014 or alphabetical full month, numerical full year.  Thank you in advance.

    <?php

$query = sqlsrv_query($conn, $sql);if ($query === false){  exit("<pre>".print_r(sqlsrv_errors(), true));}while ($row = sqlsrv_fetch_array($query)){  echo "<tr>
    <td><a href='view_invoice.php?meter_id=$meter_id&subaccount=$subaccount&start_date=$row[start_date]'>$row[meter_id]</a></td>
    <td>" . date("F Y",strtotime($row['start_date']))  . "<td>$row[invoice_no]</td><td>" . date_format($row['invoice_date'],'Y-m-d') . "<td>$row[amount_paid]</td>" . "<td>$row[payment_date]</td>" . "<td>$row[check_number]</td>" 
  ;}sqlsrv_free_stmt($query);
  ?>
Edited by Butterbean

This should do it.

$todayDate = 
date("Y-m-d");
$dateplusamonth = 
strtotime(date("Y-m-d", strtotime($todayDate)) . "+1 month");
 
echo 
date('F Y', $dateplusamonth);
 
 
 
Good luck.

Thank you for your help.  In this case, 'start_date' is a date that is manually entered into php to query mssql for data.  I'm using this start date in addition to this purpose to signify the month the bill was produced (which is at the end the 30 days that was queried by the bill.  Unfortunately, i did not create an end_date..so now I need to add 1 month to the queried 'start_date' if that makes sense.   The purpose for what Im doing it simply to prevent confusion in saving bill copies.  Currently we are calling a bill with a start_date of January...a January bill.  We really need to call it a February bill.  Will this still work in this situation? 

Edited by Butterbean
Based on what I see, I tried this... no luck.

 

$query = sqlsrv_query($conn, $sql);if ($query === false){  exit("<pre>".print_r(sqlsrv_errors(), true));}while ($row = sqlsrv_fetch_array($query)){  echo "<tr>

    <td><a href='view_invoice.php?meter_id=$meter_id&subaccount=$subaccount&start_date=$row[start_date]'>$row[meter_id]</a></td>

    <td>" . date("F Y",strtotime($row['start_date']))  . "+1 month")  . "</td><td>$row[invoice_no]</td><td>" . date_format($row['invoice_date'],'Y-m-d') . "</td><td><a href='approve_payment.php?meter_id=$meter_id&subaccount=$subaccount&start_date=$row[start_date]'>Pay Invoice</a></td>

  </tr>";}sqlsrv_free_stmt($query);

  ?> 



$query = sqlsrv_query($conn, $sql);if ($query === false){ exit("<pre>".print_r(sqlsrv_errors(), true));}while ($row = sqlsrv_fetch_array($query)){ echo "<tr>
<td><a href='view_invoice.php?meter_id=$meter_id&subaccount=$subaccount&start_date=$row[start_date]'>$row[meter_id]</a></td>
<td>" . date("F Y",strtotime($row['start_date'])) . "+1 month") "</td><td>$row[invoice_no]</td><td>" . date_format($row['invoice_date'],'Y-m-d') . "</td><td><a href='approve_payment.php?meter_id=$meter_id&subaccount=$subaccount&start_date=$row[start_date]'>Pay Invoice</a></td>
</tr>";}sqlsrv_free_stmt($query);
?>

  • Solution

Use the method I supplied to create the correct date+1mnth (the first 2 steps) and then just include the variable you've created in your output. Let me know if that works.

 

edit: like this

 

$query = sqlsrv_query($conn, $sql);if ($query === false){ exit("<pre>".print_r(sqlsrv_errors(), 
true));}
while ($row 
= 
sqlsrv_fetch_array($query)){ 

$thedatetouse = $row['start_date'];
$dateplusamonth = strtotime(date("Y-m-d", 
strtotime($thedatetouse)) . "+1 month"); 

 
echo "<tr> <td><a 
href='view_invoice.php?meter_id=$meter_id&subaccount=$subaccount&start_date=$row[start_date]'>$row[meter_id]</a>
</td> 
<td>" 
. 
date("F Y", 
$dateplusamonth) . "</td><td>$row[invoice_no]</td>
<td>" 
. date_format($row['invoice_date'],'Y-m-d') . "</td>
<td><a 
href='approve_payment.php?meter_id=$meter_id&subaccount=$subaccount&start_date=$row[start_date]'>Pay 
Invoice</a></td>
 </tr>";}
sqlsrv_free_stmt($query); 
?>
 
Edited by wezhind

CAUTION when add time periods to dates at the end of the month

$dateToUse = '2015-01-31';
$plus1month = date('Y-m-d', strtotime("$dateToUse +1 months"));

echo $plus1month;        //-->  2015-03-03 !!!

[edit]

I don't know how sqlserver handles it but MySQL gives a more natural result

mysql> SELECT '2015-01-30' + INTERVAL 1 MONTH;
+---------------------------------+
| '2015-01-31' + INTERVAL 1 MONTH |
+---------------------------------+
| 2015-02-28                      |
+---------------------------------+
Edited by Barand
  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.