I am trying to subtract one month from the current receiving year-month (2019-03)
echo $payroll['month'];
echo $newdate = date("Y-m", strtotime("-1 months",$payroll['month']));
but it through error as
2019-03
A PHP Error was encounteredSeverity: Notice
Message: A non well formed numeric value encountered
what I want 2019-03 to subtract one month so I will get 2019-02
You can add or subtract a number of days to or from a date by using a simple formula, or you can use worksheet functions that are designed to work specifically with dates in Excel.
You can try the following solution:
date('Y-m', strtotime($payroll['month'] . ' - 1 month'))
Try this -
echo $newdate = date('Y-m', strtotime('-1 months', strtotime($payroll['month'])));
So many nice answers here.
Just note that this can be done with PHP's DateTime class.
$date = new DateTime("2019-03-03");
$date->sub(new DateInterval('P1M')); // P -> Period 1 Month
echo $date->format("Y-m")
// Outputs: 2019-02
A quick note from strtotime() 's official documentation.
Note: Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With