D
D
del9937882016-11-18 14:25:56
PHP
del993788, 2016-11-18 14:25:56

How to send multiple dates to db?

Hello. My column is of type DATE. But for some reason php does not process this code correctly:

$from = new DateTime('2016-01-31');
$to   = new DateTime('2016-02-04');
 
$period = new DatePeriod($from, new DateInterval('P1D'), $to);
 
$array_dates = array_map(
    function($item){return $item->format('Y.m.d');},
    iterator_to_array($period)
);

$date_string = implode(',',$array_dates);
mysqli_query($connect, "INSERT INTO `date` VALUES (".$date_string.")");

For some reason, one line with the content 0000-00-00 is filled in the database, although there should be several lines with dates. Why do I get such an error?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2016-11-18
@webinar

php has nothing to do with
DATE - it stores the date value as YYYY-MM-DD. For example, 2008-10-23.
Why did you decide that you can write several dates there separated by commas?
If you have several dates, then there should be several cells with the DATE type.
0000-00-00 is the default value for the DATE type, which is set every time an attempt is made to write heresy there.
However, you can pervert and store dates separated by commas in one cell, but of type STRING

E
Edward, 2016-11-18
@edb

$date_string = "('" . implode("'), ('", $array_dates) . "')";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question