这是日期,基于这个想法,它将根据charge_installment值将日期增加到每月的15号和每月的结束日期。
此问题与本节不同> How to get every 15th and last day of the month in PHP
$date = '21-JAN-19';
$chrg_installment = 4;
for ($i=1; $i <= $chrg_installment ; $i++) {
echo $date;
}
此循环的输出。
19年1月21日 19年1月21日 19年1月21日 19年1月21日
正确的输出应为:
答案 0 :(得分:1)
要查找月份的最后一天,可以使用t
作为提供给date()
函数的format参数。要查找每月的15号,请使用mktime
生成时间,并将其转换为具有所需输出格式的日期。
/* Initial date and duration of processing */
$start = '2019-01-21';
$months = 4;
/* reduce start date to it's constituent parts */
$year = date('Y',strtotime($start));
$month = date('m',strtotime($start));
$day = date('d',strtotime($start));
/* store results */
$output=array();
for( $i=0; $i < $months; $i++ ){
/* Get the 15th of the month */
$output[]=date('Y-m-d', mktime( 0, 0, 0, $month + $i, 15, $year ) );
/* Get the last day of the calendar month */
$output[]=date('Y-m-t', mktime( 0, 0, 0, $month + $i, 1, $year ) );
}
/* use the results somehow... */
printf('<pre>%s</pre>',print_r($output,true));
输出:
Array
(
[0] => 2019-01-15
[1] => 2019-01-31
[2] => 2019-02-15
[3] => 2019-02-28
[4] => 2019-03-15
[5] => 2019-03-31
[6] => 2019-04-15
[7] => 2019-04-30
)
如果,如下面的注释所建议的那样,您希望日期以月末开始,而不是该月的15号,只需更改将计算的日期添加到输出数组的循环内的顺序即可。>
for( $i=0; $i < $months; $i++ ){
/* Get the last day of the calendar month */
$output[]=date('Y-m-t', mktime( 0, 0, 0, $month + $i, 1, $year ) );
/* Get the 15th of the month */
$output[]=date('Y-m-d', mktime( 0, 0, 0, $month + $i, 15, $year ) );
}
输出:
Array
(
[0] => 2019-01-31
[1] => 2019-01-15
[2] => 2019-02-28
[3] => 2019-02-15
[4] => 2019-03-31
[5] => 2019-03-15
[6] => 2019-04-30
[7] => 2019-04-15
)
以示例结果的确切格式输出结果
$format=(object)array(
'last' => 't-M-y',
'15th' => 'd-M-y'
);
for( $i=0; $i < $months; $i++ ){
/* Get the last day of the calendar month */
$output[]=date( $format->{'last'}, mktime( 0, 0, 0, $month + $i, 1, $year ) );
/* Get the 15th of the month */
$output[]=date( $format->{'15th'}, mktime( 0, 0, 0, $month + $i, 15, $year ) );
}
输出:
Array
(
[0] => 31-Jan-19
[1] => 15-Jan-19
[2] => 28-Feb-19
[3] => 15-Feb-19
[4] => 31-Mar-19
[5] => 15-Mar-19
[6] => 30-Apr-19
[7] => 15-Apr-19
)
答案 1 :(得分:0)
$chrg_installment = 3;
$result=array();
$new_Date="";
for ($i=1; $i <= $chrg_installment ; $i++) {
if($i==1){
$date='21-JAN-19';
$date=date("t-M-Y", strtotime($date));
array_push($result, $date);
$new_Date= date("d-M-Y", strtotime("+15 day", strtotime($date)));
array_push($result, $new_Date);
}
else{
$date=date("d-M-Y",strtotime('first day of this month'));
$date=date("t-M-Y", strtotime($new_Date));
array_push($result, $date);
$new_Date= date("d-M-Y", strtotime("+15 day", strtotime($date)));
array_push($result, $new_Date);
}
}
print_r($result);
答案 2 :(得分:0)
public class Student {
private int studentnr;
private String voornaam;
private String achternaam;
private LocalDate geboortedatum;
private Adres adres;
/**
* Default Constructor maakt een student aan
* @param studentnr
* Studentnr wordt meegegeven in main
* @param voornaam
* Voornaam wordt meegegeven in main
* @param achternaam
* Achternaam wordt meegegeven in main
* @param geboortedatum
* Geboortedatum wordt meegegeven in main
* @param adres
* Adres wordt meegegeven in main
*/
public Student(int studentnr, String voornaam, String achternaam, String geboortedatum, Adres adres) {
this.studentnr = studentnr;
this.voornaam = voornaam;
this.achternaam = achternaam;
this.geboortedatum = LocalDate.parse(geboortedatum, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
this.adres = adres;
}