Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
两个灵活日期之间的周数-PHP_Php - Fatal编程技术网

两个灵活日期之间的周数-PHP

两个灵活日期之间的周数-PHP,php,Php,我已经仔细阅读了之前所有与这个问题类似的问题,不幸的是,没有一个对我有用 我想知道两次约会之间的周数 $result = mysqli_query($con,"SELECT * FROM `test` WHERE `DateofTest` BETWEEN '" . $startDate . "' AND '" . $endDate . "' ") or die ("Error: ".mysqli_error($con)); $startDate = $_POST['start']

我已经仔细阅读了之前所有与这个问题类似的问题,不幸的是,没有一个对我有用

我想知道两次约会之间的周数

$result = mysqli_query($con,"SELECT * FROM `test` WHERE  `DateofTest` BETWEEN  '" .    
$startDate . "' AND  '" . $endDate . "' ") or die ("Error: ".mysqli_error($con));

$startDate = $_POST['start'];
$endDate = $POST['end']; 
假设我的开始日期是2014年12月1日,结束日期是2014年12月31日,因此需要4周

这是我的密码

   $startDate ="2014-12-01";
   $endDate ="2014-12-31";
   $days=($startDate - $endDate);
     echo $days;
   $weeks=($days / 7);
   echo $weeks;
我每天和每周都得到0个结果

有什么想法吗


谢谢你不能计算字符串。您需要将它们转换为日期。 您可以执行以下操作:

function get_number_of_weeks($startDate, $endDate) {
    // use strtotime and substract the end date from the start date, not the otherway around
    $days = strtotime($endDate) - strtotime($startDate);

    // devide by seconds / hours and weeks
    $weeks = $days / 3600 / 24 / 7;
    // floor the amount of weeks.
    return floor($weeks);
}

echo get_number_of_weeks("2014-12-01", "2014-12-31");

使用date-time对象可以实现类似的功能

$d1 = new DateTime("2014-12-01");
$d2 = new DateTime("2014-12-31");
$difference_in_days = $d1->diff($d2)->days;

echo "Diff in Weeks = ".$difference_in_days/7;

您不能比较日期字符串并期望得到差异。 应使用DateTime类比较两个DateTime值:

$startDate = new DateTime("2014-12-01");
$endDate = new DateTime("2014-12-31");
$diff = $startDate->diff( $endDate )->format('%d');
$weeks = floor($diff/7);
format
方法可以以多种方式返回差异,如年/月/日/小时/分钟/秒。更多

试试这个

    <?php

    $d1 ="2014-12-01";
       $d2 ="2014-12-31";

    $diffweek = abs(strtotime($d1) - strtotime($d2)) / 604800;
    echo round($diffweek); or echo intval($diffweek);
  ?>


这是因为您使用的是字符串而不是日期进行计算,所以我测试您的代码。不正确。我得到了周差=4.2857142857143。没错,31天差是4.28周,如果你想用你更熟悉的数字,那么用floor()四舍五入,这样它会说“4周”,而不是4周。2@EmJad这当然是正确的。您只需要忽略任何小数点。在函数中可以这样做,因此类似于函数getnumberofweeks($startDate,$endDate){$days=strtotime($endDate)-strtotime($startDate);//按秒/小时和周进行设置$weeks=$days/3600/24/7;返回楼层($weeks)}?我用我的代码作为函数进行测试,但没有得到任何结果:($result=mysqli_query($con,“SELECT*FROM
test
WHERE
DateofTest
介于“.$startDate.”和“.$endDate.””之间)或死($Error:.mysqli_Error($con));函数getNumberofWeeks($startDate,$endDate){$days=strottime($endDate)-strotime($startDate);//按秒/小时和周划分$weeks=$days/3600/24/7;//下限周数。返回下限($weeks);}我想你需要把变量传递给函数吗?是的,这正是我想做的。有可能在函数中这样做,因为某种原因,当我把它放到函数中时,我无法让它工作。$result=mysqli_query($con,“SELECT*FROM
test
WHERE
Date
BETWEEN'.$startDate.'AND'.$endDate')或者死(“Error:.mysqli_Error($con));函数getNumberofWeeks($startDate,$endDate){$startDate=$\u POST['start'];$endDate=$\u POST['end'];$diffweek=abs(strotime($startDate)-strotime($endDate))/604800;返回回合($diffweek);}有没有可能因为某种原因在函数中执行此操作?当我将其放入函数中时,我无法使其工作。$result=mysqli_query($con,“选择*从测试中,日期介于“.$startDate.”和“.$endDate.”之间)或死亡($Error:.mysqli_Error($con));函数getNumberofWeeks($startDate,$endDate){$startDate=$\u POST['start']$endDate=$_POST['end'];$diffweek=abs(strotime($startDate)-strotime($endDate))/604800;返回回合($diffweek);}