06/02/2017 12:49pm

PHP | Problem -


    public function dateValidation($DateValue, $month = "change_month"){

if(!$DateValue) return false;
$Date = DateTime::createFromFormat('Y-m-d', $DateValue);

switch ($month){
case "change_month":
if ($Date->format('Y-m-d') == $Date->format('Y-m-t')) {
$ModifiedDate = $Date->modify("last day of last month")->format('Y-m-d');
} else if($Date->format('m') == DateTime::createFromFormat('Y-m-d', $DateValue)->modify("-1 month")->format('m')){
$ModifiedDate = $Date->modify("last day of previous month")->format('Y-m-d');
} else {
$ModifiedDate = $Date->modify("-1 month")->format('Y-m-d');
}
break;
case "change_month3":
if ($Date->format('Y-m-d') == $Date->format('Y-m-t')) {
$ModifiedDate = $Date->modify("last day of -3 months")->format('Y-m-d');
} else if(DateTime::createFromFormat('Y-m-d', $DateValue)->modify("-3 months")->format('m') != DateTime::createFromFormat('Y-m-d', $DateValue)->modify("first day of -3 months")->format('m')){
$ModifiedDate = $Date->modify("last day of -3 months")->format('Y-m-d');
} else {
$ModifiedDate = $Date->modify("-3 months")->format('Y-m-d');
}
break;
case "change_month6":
if ($Date->format('Y-m-d') == $Date->format('Y-m-t')) {
$ModifiedDate = $Date->modify("last day of -6 months")->format('Y-m-d');
} else if(DateTime::createFromFormat('Y-m-d', $DateValue)->modify("-6 months")->format('m') != DateTime::createFromFormat('Y-m-d', $DateValue)->modify("first day of -6 months")->format('m')){
$ModifiedDate = $Date->modify("last day of -6 months")->format('Y-m-d');
} else {
$ModifiedDate = $Date->modify("-6 months")->format('Y-m-d');
}
break;
case "change_year":
if ($Date->format('Y-m-d') == $Date->format('Y-m-t')) {
$ModifiedDate = $Date->modify("last day of -1 year")->format('Y-m-d');
} else {
$ModifiedDate = $Date->modify("-1 year")->format('Y-m-d');
}
break;
case "change_year3":
if ($Date->format('Y-m-d') == $Date->format('Y-m-t')) {
$ModifiedDate = $Date->modify("last day of -3 year")->format('Y-m-d');
} else {
$ModifiedDate = $Date->modify("-3 year")->format('Y-m-d');
}
break;
case "change_year5":
if ($Date->format('Y-m-d') == $Date->format('Y-m-t')) {
$ModifiedDate = $Date->modify("last day of -5 year")->format('Y-m-d');
} else {
$ModifiedDate = $Date->modify("-5 year")->format('Y-m-d');
}
break;
case "change_year10":
if ($Date->format('Y-m-d') == $Date->format('Y-m-t')) {
$ModifiedDate = $Date->modify("last day of -10 year")->format('Y-m-d');
} else {
$ModifiedDate = $Date->modify("-10 year")->format('Y-m-d');
}
break;
}

return $ModifiedDate;
}