Problem when sending a range of php dates

1

I have two input that passes a range of dates to the program in php that I have to return some ids, the problem is that it does not return anything, all in white:

Desde Fecha <input name="desde_fecha_cumple" type="date" value="<?php echo date("Y-m-d"); ?>"/><br />
Hasta fecha <input name="hasta_fecha_cumple" type="date" value="<?php echo date("Y-m-d"); ?>"/><br />

These are the inputs, and the program in php:

function ids_cumplen_anos($fecha) {
  $link = mysql_connect("localhost", "XX", "XXX");
  mysql_select_db("XX_database", $link);
  $sql = "select id from usuario where day(FechaNacimiento)>=day('" . $fecha . "') and month(FechaNacimiento)>=month('" . $fecha . "') AND day(FechaNacimiento)<=day('" . $fecha ."') and month(FechaNacimiento)<=month('" . $fecha ."')"; //funcion sacar cumple
  $result = mysql_query($sql, $link);
  $socios = array();
  if ($result) {
    //echo "hay resultados";
    while ($row = mysql_fetch_assoc($result)) {
        $socios[] = $row['id'];
    }
  }
  if (!count($socios))
    $socios = 0;

  return $socios;
}

This has to generate an excel with the following code:

"datos" => obtener_socios(ids_cumplen_anos($datos['desde_fecha_cumple'], $datos['hasta_fecha_cumple'], $datos['opt']));

How can I make the range of dates so that both are passed to the program in php and this is printed in the excel?

I made an echo of $ sql and it only returns a date

In the first input I indicated 02/03/2017 and in the second input 04/04/2017

This is the query:

  

select id from user where day (Birthdate) > = day ('2017-03-02')   and month (Birthdate) > = month ('2017-03-02') and   day (Birthdate) < = day ('2017-03-02') and   month (Birthdate) < = month ('2017-03-02')

The same first value of the first input in all, does not take the second value of the second input.

Edit2-- You already take both values from the dates but you do not paint the ids of the partners that have years between that range.

    
asked by Rubencio322 05.04.2017 в 17:03
source

3 answers

2

Good the query would do it in the following way to obtain data according to range of dates.

SELECT id FROM usuario WHERE FechaNacimiento BETWEEN '2017-03-02' and '2017-
03-04';

If you are sending a string with the format '% d /% m /% Y' you need to convert it and format it so that in this way you can obtain the users that are years old according to the range of dates you indicated.

SELECT id  FROM usuario 
WHERE STR_TO_DATE(DATE_FORMAT(FechaNacimiento, '%m/%d'), '%m/%d')
BETWEEN STR_TO_DATE('02/03/2017', '%d/%m')
AND STR_TO_DATE('04/03/2017', '%d/%m')

Greetings!

    
answered by 05.04.2017 в 18:03
0

Hello for you to work the date range, you have to place two different dates. For what you are showing in your code are the same since you are using the date php method in both fields. Here are some examples:

  

Get all the people who were born on a specific day:

select id from user where Birthdate = '2000-05-04';

  

Obtain all persons between 2000-05-04 and 2017-04-05

select id from usuario where FechaNacimiento >= '2000-05-04' AND FechaNacimiento <= '2017-04-05'
    
answered by 05.04.2017 в 17:58
0

The problem is that you are only passing a date to your function and then you are looking for the parameters of a single date. You would have to pass both dates. For example:

function ids_cumplen_anos($fecha1,fecha2) {
  $link = mysql_connect("localhost", "XX", "XXX");
  mysql_select_db("XX_database", $link);
  $sql = "select id from usuarios where FechaNacimiento between '".$fecha1."' and '".$fecha2."'"; //funcion sacar cumple
  $result = mysql_query($sql, $link);
  $socios = array();
  if (mysql_num_rows($result)>0) {
    //echo "hay resultados";
    while ($row = mysql_fetch_assoc($result)) {
        $socios[] = $row['id'];
    }
  }
  else{
$socios = 0;
}

  return $socios;
}

PS: There is no need to cast the date to days, months or years, unless you need other information. Blasito is right, you have to see that the format you are sending corresponds to the one stored in the DB, if it is different you have to cast it.

Greetings

    
answered by 05.04.2017 в 20:43