3

I'm new to PHP Arrays... but I have created an array to get all dates between 2 dates that have been submitted in a form.

See below:

$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;
}

This results in the below

Array ( [0] => 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 )

Is there a way to save each of these dates to a MySQL database using PHP?

4 Answers 4

1

After you get the dates from range do loop to insert each value of the array.

$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;
        }
    }   
Sign up to request clarification or add additional context in comments.

Comments

0

You can save array in mysql by using serialize / unserialize

Example -

$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

You can also use implode and explode.

Example -

<?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);
?>

Comments

0

You can save array in mysql by using json_encode / json_decode. It's easy to use.

like following code:

<?php
    $dates = array(
         '2016-10-10', '2016-10-11'
    );
    $json_dates = json_encode($dates);
    $arr = json_decode($json_dates, true);
?>

Comments

0

You cannot store array in database but alternative solution is to convert array to JSON string.

You can convert your array to json & store into database.

$dates_in_json = json_encode($dates);
//Insert query
$insert = mysql_query("INSERT INTO table('dates') VALUES('".$dates_in_json ."')");

You can get this data anywhere and convert to array like this

$dates_array = json_decode($dates_in_json, TRUE);

here second parameter TRUE for convert into array, if you will not provide TRUE json will be converted to PHP Object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.