用PHP计算日期范围内的天数?

时间:2018-07-07 07:58:45

标签: php jquery html wordpress function

我想使用一个函数来输出日期范围内的天数

为此,我使用类似下面的内容;

~/Documents/ElectoralRollPDFs/completed/Baharagora/*.pdf
I/O Error: Couldn't open file '~/Documents/ElectoralRollPDFs/completed/Baharagora/*.pdf': No such file or directory.

但是,当我退出$from = $_POST['from']; $to = $_POST['to']; $numdays = date_diff($to,$from); 时,它始终为“ 0”。我该如何正确处理?

任何线索?

2 个答案:

答案 0 :(得分:1)

您必须将字符串转换为日期格式以进行计算,然后再转换回字符串以显示它

<?php
if(isset($_POST['from']) && isset($_POST['to'])){
    $from = date_create($_POST['from']);
    $to  = date_create($_POST['to']);
    $numdays = date_diff($from,$to);
    echo $numdays->format("%r%a days");

}

?>

<form action="" method="post">
<lable for="from">from</lable>
<input type="date" name="from" id="from">
<lable for="to">to</lable>
<input type="date" name="to" id="to">
<input type="submit">

答案 1 :(得分:0)

首先使用date_create为日期创建日期对象,然后计算差异为:

$numdays = date_diff(date_create($_POST['from']), date_create($_POST['to']))->format('%a');

详细了解date_diff