php - 使用 while 循环插入数据库

标签 php mysql database while-loop do-while

我想创建一个函数来选择开始日期和结束日期,然后插入数据库。我想将 30/8 到 30/9 每 4 天的日期插入数据库...

例如:

table 
row 1 = 30/8 
row 2 = 3/9
row 3 = 7/9 
row 4 = 11/9 ... and so on 

我的PHP:

<?php
$con = mysql_connect("localhost", "root", "root");
mysql_select_db("test", $con);

$start_date= "2015-08-30";
$end_date= "2015-09-30";

$insert = "INSERT INTO calendar(date)......";

if(mysql_query($insert,$con)) {
    echo "<script> alert('Insert Successful'); </script>";
}
else {
    echo "<script> alert('Insert Unsuccessful'); </script>";
}

while (strtotime($start_date) <= strtotime($end_date)) {
    //echo "$start_date <br>";
    $start_date = date ("Y-m-d", strtotime("+4 day", strtotime($start_date)));
}

最佳答案

认为这就是您正在寻找的...让我知道需要更改的任何内容...

    <?php

$start_date= "2015-08-30";
$end_date= "2015-09-30";

$inc_val = 4;

$start_con_to_time = strtotime($start_date);
$end_con_to_time = strtotime($end_date);

$days_between = ceil(abs($end_con_to_time - $start_con_to_time) / 86400);

for($x=$inc_val+1; $x<=$days_between;)
{
    echo date('Y-m-d', strtotime($start_date. ' + ' . $x . 'days'));
    echo ' : Add database code here <br>';
    $x+=$inc_val;
}

?>

关于php - 使用 while 循环插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32774045/

相关文章:

mysql - 如何在 MySQL 中使用 max 和 having

PHP Mysql - 显示每个月的休假记录并明智地休假类型

android - Android studio数据库中如何创建多个表

PHP 框架 : Locked In?

Javascript - 计算数组中复选框的总和

javascript - 多个复选框 - 更改消息 "Please check this box if you want to proceed"

php - 在 php 中将 WordPress 特色图片转换为背景图片

MySQL Left Join 返回错误

我心目中的 MySQL 性能问题

sql - 跨多个表的参照完整性