Thursday 8 October 2020

Generate data format from current date to x months

 /**

 * Generate data format from current date to x months
*
* @param $months
*
* @return DateTime|string
*
* @throws Exception
*/
function generateDateByMonths($months)
{
$date = '';
$time = '23:59:59';
if(is_numeric($months) && $months > 0) {
$date = new DateTime('now');
$date->modify('+'.$months.' month'); // or you can use '-2 months' for deduct
$date = $date->format('Y-m-d');
}
return $date . ' ' . $time;
}