我对PHP数组很陌生..。但是我已经创建了一个数组来获取两个以表单形式提交的日期之间的所有日期。
见下文:
$endDate=$_POST[todate];
$startDate =$_POST[fromdate];
print_r(getDatesFromRange( "$datef", "$dateto" ));
function getDatesFromRange($startDate, $endDate)
{
$return = array($startDate);
$start = $startDate;
$i=1;
if (strtotime($startDate) < strtotime($endDate))
{
while (strtotime($start) < strtotime($endDate))
{
$start = date('Y-m-d', strtotime($startDate.'+'.$i.' days'));
$return[] = $start;
$i++;
}
}
return $return;
}这将导致以下结果
阵列( => 2016-10-10 1 => 2016-10-11 2 => 2016-10-12 3 => 2016-10-13 4 => 2016-10-14 5 => 2016-10-15 6 => 2016-10-16 7 => 2016-10-17 8 => 2016-10-18 9 => 2016-10-19 )
有没有一种方法可以使用PHP将每个日期保存到MySQL数据库?
发布于 2016-10-08 11:02:51
在获得range的日期之后,do循环插入数组的每个值。
$date = getDatesFromRange( "$datef", "$dateto" );
foreach ($date as $d){
$sql = "INSERT INTO MyDatabase (dateCol)
VALUES ('$d')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
} 发布于 2016-10-08 10:59:29
可以使用串行化 / 非序列化在mysql中保存数组
示例-
$array = array("my", "litte", "array", 2);
$serialized_array = serialize($array);
$unserialized_array = unserialize($serialized_array);
var_dump($serialized_array); // gives back a string, perfectly for db saving!
var_dump($unserialized_array); // gives back the array again您还可以使用内爆和implode。
例子-
<?php
$vegetables[0] = "corn";
$vegetables[1] = "broccoli";
$vegetables[2] = "zucchini";
$text = implode(",", $vegetables);
echo $text;
?>
<?php
$text = "corn, broccoli, zucchini";
$vegetables = explode(", ", $text);
print_r($vegetables);
?>发布于 2016-10-08 11:03:52
您可以使用json_encode / json_decode在mysql中保存数组。它很容易使用。
如下代码所示:
<?php
$dates = array(
'2016-10-10', '2016-10-11'
);
$json_dates = json_encode($dates);
$arr = json_decode($json_dates, true);
?>https://stackoverflow.com/questions/39931514
复制相似问题