Date by rank

0

How can I make a query of dates by rank if the date is 2017-09-04 - 2017-10-03 ----- > This is how the daterangepicker throws it at me. and since I do not have them separated I can not do a BETWEEN.

    
asked by Magnolia Eve 03.10.2017 в 23:36
source

2 answers

0

The only way I see doing this is by separating the two dates, and the only "brand" to call it so I have to separate them is "-" taking into account the spaces since the dates also have scripts. Having said that, here is my solution proposal.

First doing tests determine how to separate the dates

select substring('2017-09-04 - 2017-10-03',1,instr('2017-09-04 - 2017-10-
03',' - '));

select ltrim(substring('2017-09-04 - 2017-10-03',instr('2017-09-04 - 
2017-10-03','- ')+1));

Note in the second select the '-'

Then the only thing left is to add this in a select. I for a practical and legibility question would store each of the dates in variables and then I would use them in a select

set @fecha1 = (select substring('2017-09-04 - 2017-10-
03',1,instr('2017-09-04 - 2017-10-03',' - ')));
set @fecha2 = (select ltrim(substring('2017-09-04 - 2017-10-
03',instr('2017-09-04 - 2017-10-03','- ')+1)));

select * from tabla where campoFecha between @fecha1 and @fecha2
    
answered by 06.10.2017 в 23:32
0

In the labels of your question, php is not included, so users could have thought that you wanted to solve it only with MySQL . But seeing that in your comments you say that you have solved it with code php , then it is understood that php is the server language that you are using. The best thing in this case is to take the string that reaches you from dateRangePicker and manipulate it to get the start date and the end date and in this way to be able to query sql . I see by your comments that you solved using explode . That would be a solution, but you have some more:

  

These solutions are valid if you ensure that the dateRangePicker will always deliver the dates in that format, you must test your code very well in different environments.

1 - Using explode (the solution you used)

$fecha  = $_POST['fecha'];
$array  = explode(' - ', $fecha);
$fecha1 = $array[0];
$fecha2 = $array[1];

Ideone example

2 - Using preg_replace and a regular expression that matches the two dates capturing each in a group

$fecha  = $_POST['fecha'];
$reg    = '/^([^\s]+)[^\d]+([^\s]+)$/';
$fecha1 = preg_replace($reg, '$1', $fecha);
$fecha2 = preg_replace($reg, '$2', $fecha);

Ideone example

3 - Using preg_match_all and a regular expression that matches any string that does not contain spaces

$fecha  = $_POST['fecha'];
$fechas = preg_match_all('/[^\s]+/', $fecha, $matches);
$fecha1 = $matches[0][0];
$fecha2 = $matches[0][2];

Ideone example

4 - Using substr

$fecha  = $_POST['fecha'];
$fecha1 = substr($fecha, 0, 10);
$fecha2 = substr($fecha, 13, 10);

Ideone example

    
answered by 12.11.2017 в 03:58