I'm trying to write a PHP code that's a bit complex for me...
I'd like when data is posted to my PHP file it check the today date and if the current date if after the fifth of the month, the code will echo the fifth of next month. Like :
$today = "2018-04-24";
if(today>5){
echo "2018-05-05";
}
I hope I'm making myself comprehensible.
Thanks
You need a proper date handling to handle edge cases like 2018-12-24
.
$today = "2018-03-31";
$dt = DateTime::createFromFormat('Y-m-d', $today);
$d = intval($dt->format('j'));
if($d > 5) {
$dt->setDate($dt->format('Y'), $dt->format('n'), 5);
$dt->add(new DateInterval('P1M'));
}
echo $dt->format('Y-m-d');
function get_month_diff($start, $end = FALSE){
$end OR $end = time();
$start = new DateTime("@$start");
$end = new DateTime("@$end");
$diff = $start->diff($end);
return $diff->format('%y') * 12 + $diff->format('%m');}
you can do like this
$today = date('Y-m-d');
$exploded_date = explode('-',$today);
if($exploded_date[2] > 5){
echo date('Y-m-05', strtotime("+1 months", strtotime($today)));
}
After the fifth of the month, the code will echo the fifth of next month, you can use this :
$effectiveDate = "2018-02-06";
if(date('d') > 5) {
$effectiveDate = date('Y-m-d', strtotime("+5 months", strtotime($effectiveDate)));
}