无法解析通过PHP表单提交的日期

guicsvcw  于 2022-11-28  发布在  PHP
关注(0)|答案(1)|浏览(123)

我设计了一个表单,用户使用该表单提交两个日期。然后使用“echo”将这些日期打印在屏幕上。之后,我尝试创建一个数组,其中包含“Y.m.d”格式的这两个日期范围的值。我为此编写了一个自定义函数。当我将日期传递给该函数时,该函数工作正常。但是,当我使用变量将用户提交的日期传递给该函数时,出现以下错误:
致命错误:未捕获的异常:日期时间::__构造():无法解析位置5(1)处的时间字符串(2022.11.23):第29行上的C:\wamp64\www\davinci\transit_stackoverflow_question.php中的双倍时间规范
如果您能告诉我哪里出错了,以及如何解决这个错误,我将不胜感激。谢谢。

<html>
<head>
<title>Search</title>
</head>
<body>

<?php $begindate = $_POST['begindate'];
$begindate= date("Y.m.d", strtotime($begindate));
 echo "Begin date: ". $begindate; 

  ?>
      <br><br>
<?php $enddate = $_POST['enddate'];
$enddate= date("Y.m.d", strtotime($enddate));
  echo "End date: ". $enddate; ?>
      <br><br>

<?php

// Function to get all the dates in given range
function getDatesFromRange($start, $end, $format = "Y.m.d")
{
  // Declare an empty array
  $array = array();
  // Variable that store the date interval
  // of period 1 day

  $interval = new DateInterval('P1D');
  $realEnd = new DateTime($end);
  $realEnd->add($interval);

  $period = new DatePeriod(new DateTime($start), $interval, $realEnd);

  // Use loop to store date into array
  foreach($period as $date) {
    $array[] = $date->format($format);
  }

  // Return the array elements
  return $array;
}

$date1 = date("Y-m-d");

$range = getDatesFromRange('18.01.2022','18.02.2022');
var_dump($range);

$range2 = getDatesFromRange($begindate,$enddate);
var_dump($range2);

?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  

Begin search from date: <input type="date" name="begindate" />
<BR><BR>
End search on date: <input type="date" name="enddate" />
&nbsp;&nbsp;&nbsp;&nbsp;
<BR><BR>

<input type="submit" name="submit" value="Submit">  
</form>

</body></html>
z31licg0

z31licg01#

<html>
<head>
<title>Search</title>
</head>
<body>

<?php 

$begindate = $_POST['begindate'];
echo "Begin date: ". $begindate; 

?>

<br><br>

<?php 

$enddate = $_POST['enddate'];
echo "End date: ". $enddate; 

?>

<br><br>

<?php

// Function to get all the dates in given range
function getDatesFromRange($start, $end, $format = "Y.m.d")

{

// Declare an empty array
$array = array();
// Variable that store the date interval
// of period 1 day

$interval = new DateInterval('P1D');
$realEnd = new DateTime($end);
$realEnd->add($interval);

$period = new DatePeriod(new DateTime($start), $interval, $realEnd);

// Use loop to store date into array
foreach($period as $date) {
$array[] = $date->format($format);
}

// Return the array elements
return $array;
}

$range2 = getDatesFromRange($begindate,$enddate);
var_dump($range2);

?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  

Begin search from date: 
<input type="date" name="begindate" value="<?php echo date('Y-m-d'); ?>" />
<BR><BR>
End search on date: 
<input type="date" name="enddate" value="<?php echo date('Y-m-d'); ?>" />
&nbsp;&nbsp;&nbsp;&nbsp;
<BR><BR>

<input type="submit" name="submit" value="Submit">  
</form>

</body></html>

相关问题