Reputation: 71
I am trying to output the 15th day of next month, based on input provided by the user.
For example:
04/02/2016 - input by user
05/15/2016 - output after calculation
This is my code, which I'm trying:
// this value is already coverted in strtotime.
$today = $loan_data['loan_applied_date'];
$numOfDays = date('t', $today);
$base = strtotime('+'.$numOfDays.' days', strtotime(date('m/01/Y', $today)));
$day15 = date('m/01/Y', $base);
$time = strtotime($day15);
By running the above code I am getting the 1st day. How can I get the 15th?
Example scenario:
Suppose Subscription registered on 04/04/2016, so their next payment date will be 15th of next month or 05/15/2016.
Upvotes: 2
Views: 3812
Reputation: 2058
I did something similar to recurring payments, this code charge payments every 15th:
//ini_set("date.timezone", "America/New_York"); //uncomment for test timezone
function getLastDay($date=false){
return $date?date("t", strtotime($date)):date("t", strtotime(0));
}
function monthDayMatch($data = array('startDay'=>1,'day'=>1,'lastDayMonth','startMonth'=>1,'month'=>1,'frecuency'=>1)){
if(($data['startMonth']%$data['frecuency'] == $data['month']%$data['frecuency']) && ($data['startDay'] == $data['day'] || $data['startDay'] > $data['lastDayMonth'])){
return true;
}
return false;
}
$date = '04/02/2016';
$date = explode('/',$date);
$customer_date=array('month'=>$date[0],'day'=>$date[1],'year'=>$date[2],'all'=>implode('/',$date));
$resp = monthDayMatch(array(
'startMonth'=> $customer_date['month'], //initial customer month
'month' => date('m'), //current month
'frecuency' => 1, //recurring once a month
'startDay' => date('d'), //current day day
'day' => 15, //day to validate
'lastDayMonth'=>getLastDay($customer_date['all'])
));
//every 15th is true
if($resp){
echo 'payment charge';
}else{
echo 'no payment';
}
Upvotes: 0
Reputation: 14752
Just use DateTime:
// Your description says that $loan_date['loan_applied_date'] is // "already coverted in strtotime", so I assume a UNIX timestamp ... DateTime::createFromFormat('U', $loan_date['loan_applied_date']) // When you import a UNIX timestamp into DateTime, it assumes UTC, // so we need to set your timezone back to whatever it is ->setTimezone(new DateTimeZone(date_default_timezone_get())) // Add 1 month ->modify('+1 month') // Hard-code the 15th day ->format('15/m/Y');
Upvotes: 2
Reputation: 126
The solution is quite simple,using PHP you can take advantage of the flexible mktime function, as follows:
<?php
$today = strtotime("now"); //GRAB THE DATE - YOU WOULD WANT TO REPLACE THIS WITH YOUR VALUE
$year = date("Y", $today); //GET MONTH AND YEAR VALUES
$month = date("m", $today);
$month++; //INCREMENT MONTH
echo date("d/m/Y", mktime(0, 0, 0, $month, 15, $year)); //OUTPUT WITH MKTIME
?>
Upvotes: 0