PHP (Between + Convert Date)

0

I am making a screen in php where the user has the date filter to extract a report from SQL Server , but MySQL it displays the default date mm / dd / yyyy , and I need it to dd / mm / yyyy in between , does anyone know how to do this conversion?

Here is how I tried it:

SELECT p.idProcesso, p.Usr_id, p.Usr_name
  , p.Usr_CPF, p.crd_snr, t.dsPunicao
  , convert(varchar, p.dtCadastro, 103)+' '+convert(varchar, p.dtCadastro, 108) dtCadastro
  , s.dsStatusProcesso
  , p.idStatusProcesso 
FROM tmProcesso AS p
INNER JOIN tcStatusProcesso as s 
   on p.idStatusProcesso = s.idStatusProcesso
INNER JOIN tcPunicao as t 
   on p.idPunicao = t.idPunicao
WHERE p.app_id = '$var_appid'
  and p.dtCadastro between CONVERT(VARCHAR, '$var_dtini', GETDATE(), 103) 
  and CONVERT(VARCHAR, '$var_dtfim', GETDATE(), 103)"

I do not know if this is right, I would like to confirm, if it is very obvious, please forgive me.

    
asked by anonymous 03.01.2018 / 19:56

2 answers

0

You can convert the date before moving on to the query. for example:

function convertDatetoSQL($date)
{
    $date = explode('/', $date);
    return $date[1] . '/' . $date[0] . '/'. $date[2];
}

$var_dtfim = convertDateToSQL($var_dtfim);

With explode , you transform the variable into an array by saying that the delimiter is the "/" from there everything between "/" has positions in the array, so you can manipulate the date the way you want it.

What you can do is also work with the DateTime object of PHP, it would look like this:

$newDate = DateTime::createFromFormat('d/m/Y', $var_dtfim);
$var_dtfim = $newDate->format('m/d/Y');
    
03.01.2018 / 20:05
0

Well, I'm not sure what to do.

p.dtCadastro between convert (datetime, '$ var_dtini', 103)

and convert (datetime, '$ var_dtfim', 103)

Thank you guys!

    
04.01.2018 / 11:49