如何在PHP

时间:2018-05-24 12:16:42

标签: php date strtotime

我的目标是打印dueDate。 截止日期公式是月末,在我的例子中添加7天。 我怎样才能成功呢? 我对实际结果的回应是“截止日期:1970年1月8日” 我的预期结果是“截止日期: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";

提前感谢您的帮助

5 个答案:

答案 0 :(得分:3)

尝试使用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

请在此处查看:https://3v4l.org/BjUSK

答案 1 :(得分:2)

除了未定义的变量错误

之外,您的编码逻辑是完美的
$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; //每天更改日期

答案 2 :(得分:0)

我建议您使用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/2018

答案 3 :(得分:0)

月末+ 7天是我读的...如果我错了,请纠正我:

$invoice_date= '11/05/2018';
$days= 7;

$dueDate= DateTime::createFromFormat('d/m/Y', $invoice_date);
$dueDate->modify('last day of this month')->modify('+7 days');
echo $dueDate->format('d/m/Y');

发票日期11/5/2018返回7/6/2018

答案 4 :(得分:0)

$invoice_date = "11-05-2018";
$day  = 7;//in below statements used as day
$is_invoice = false;

$date = date("Y/m/d", strtotime($invoice_date));

if ($is_invoice) {
$dueDate = date('d/m/Y', strtotime($date. ' + '.$day.' days'));
} else {
$dueDate = date('Y-m-t', strtotime($date));
$dueDate = date('d/m/Y', strtotime($dueDate. ' + '.$day.' days'));
}

echo "Due date: ".$dueDate;