我的目标是打印一个dueDate。到期日公式是月底,加上我的例子中的7天。我怎么才能让它成为可能?我对实际结果的呼应值是“到期日: 08/01/1970”我的预期结果是“到期日: 07/06/2018”。
$invoice_date = "11/05/2018";
$days = 7;
$is_invoice = false;
$date = date("d/m/Y", strtotime($invoice_date));
if ($is_invoice) {
$dueDate = date('d/m/Y', strtotime("+$day $days", strtotime($date)));
} else {
$dueDate = date('t/m/Y', strtotime($date));
$dueDate = date('d/m/Y', strtotime("+$day days", strtotime($date)));
}
echo "Due date: $dueDate";提前感谢您的帮助
发布于 2018-05-24 12:23:45
尝试使用DateTime类:
$date = DateTime::createFromFormat('d/m/Y', '11/05/2018');
$dueDate = clone $date;
$dueDate->modify('+7 days');
echo 'Date : ' . $date->format('d/m/Y') . "\n";
echo 'Due : ' . $dueDate->format('d/m/Y') . "\n";输出:
Date : 11/05/2018
Due : 18/05/2018发布于 2018-05-24 12:27:31
除了未定义的变量错误外,您的编码逻辑是完美的。
$invoice_date = "11/05/2018";
$day = 7;//in below statements used as day
$is_invoice = false;
$date = date("d/m/Y", strtotime($invoice_date));
if ($is_invoice) {
$dueDate = date('d/m/Y', strtotime("+$day days", strtotime($date)));
} else {
$dueDate = date('t/m/Y', strtotime($date));
$dueDate = date('d/m/Y', strtotime("+$day days", strtotime($date)));
}
echo "Due date: $dueDate";未定义变量$ day;//每天更改日期
发布于 2018-05-24 12:23:18
我建议您使用DateTime类和相关函数:
$invoice_date = "11/05/2018";
$days = 7;
$input = date_create_from_format('d/m/Y', $invoice_date);
$result = $input->add(new DateInterval("P${days}D"));
$dueDate = $result->format('d/m/Y');
echo "Due date: $dueDate";输出:
Due date: 11/05/2018https://stackoverflow.com/questions/50509233
复制相似问题