D
D
del9937882016-11-27 16:50:38
PHP
del993788, 2016-11-27 16:50:38

How to find a range of dates from the database and display only duplicates?

Hello. There is such a code.

$nnomer = $_POST['nnomer'];
const SQL_GET_MENU_ITEM = '
SELECT datestart, dateend FROM main WHERE namenomer = :namenomer
';
try{
$pdo = new PDO("mysql:host=127.0.0.1;dbname=module;charset=utf8","root","");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $pdo->prepare(SQL_GET_MENU_ITEM);
$res = $stmt->execute([':namenomer' => $nnomer]);
$row = json_encode($stmt->fetchAll(PDO::FETCH_OBJ));
echo "$row";
}catch(PDOException  $e ){
exit( $e->getMessage());
}

The database looks like this:
1df143df96a548ca8a433400417fad81.PNG
How the script works: An ajax request with $_POST['nnomer'] leaves the site; (it is equal to lux), php substitutes it into the pdo request to the database and gets the answer. The answer is the first and last date in the date range. In my case, the answer is:
[{"datestart":"2016-11-29","dateend":"2016-12-03"},{"datestart":"2016-12-01","dateend":"2016-12-05"}]

There are 4 dates in total, but in fact there will be more.
So. Can you tell me how to find duplicate dates in php? In my case, the repeated dates are: 2016-12-01, 2016-12-02 and 2016-12-03. Is this even possible to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2016-11-27
@R0dger

Write a query where you group dates and display where count > 2
ala something like this

select count(datestart) as cnt, datestart FROM youTable
GROUP BY datestart
HAVING cnt > 1

add the condition yourself .. so that between is already

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question