I was developing a view with PHP 7 AND SQL SERVER 2012 , where the user filters the data based on an initial date, a range of dates, by beneficiary, among others.
All queries and views work without any problems locally .
When transferring the folder directly to the productive server, I had problems with the versions in php , I made the respective change of the versions and at the time of testing, some of the queries, specifically those that work with dates , are not bringing me the date.
It should be noted that I already made sure to copy and paste the codes correctly .
What would be the problem?
if($v1 == 1 && $v2 != null && $v3 != null && $v4 != null)
{
echo("
<thead>
<th>Teléfono</th>
<th>Cédula</th>
<th>Beneficiario</th>
<th>Tarjeta</th>
<th>Cliente</th>
<th>Producto</th>
<th>Fecha de Consulta</th>
</thead>
");
$consulta = "
declare @Fecha_ini datetime
declare @Fecha_fin datetime
set @Fecha_ini = '$v2'
set @Fecha_fin = '$v3'
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'-',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono,
CONCAT(SUBSTRING(Cedula,1,2),'.',SUBSTRING(Cedula,3,3),'.',SUBSTRING(Cedula,6,6)) AS Cedula,
CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre,
NumeroTarjeta as Tarjeta,
tarjetas.cliente as Cliente,
Producto.nombre as Producto,
Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '8' and replace(cedula,'.','') = $v4 and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by RegistroConsultaSaldoSMS.FechaConsulta asc) T
union all
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'- ',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono
,CONCAT(SUBSTRING(Cedula,1,1),'.',SUBSTRING(Cedula,3,3),'.',SUBSTRING(Cedula,5,5)) AS Cedula
,CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre
,NumeroTarjeta as Tarjeta
,tarjetas.cliente as Cliente
,Producto.nombre as Producto
,Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '7' and replace(cedula,'.','') = $v4 and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by RegistroConsultaSaldoSMS.FechaConsulta asc) T1
union all
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'-',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono
,CONCAT(SUBSTRING(Cedula,1,3),'.',SUBSTRING(Cedula,4,6)) AS Cedula
,CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre
,NumeroTarjeta as Tarjeta
,tarjetas.cliente as Cliente
,Producto.nombre as Producto
,Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '6' and replace(cedula,'.','') = $v4 and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by RegistroConsultaSaldoSMS.FechaConsulta asc) T2
union all
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'-',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono
,CONCAT(SUBSTRING(Cedula,1,2),'.',SUBSTRING(Cedula,3,5)) AS Cedula
,CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre
,NumeroTarjeta as Tarjeta
,tarjetas.cliente as Cliente
,Producto.nombre as Producto
,Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '5' and replace(cedula,'.','') = $v4 and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by RegistroConsultaSaldoSMS.FechaConsulta asc) T3
union all
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'-',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono
,CONCAT(SUBSTRING(Cedula,1,1),'.',SUBSTRING(Cedula,2,4)) AS Cedula
,CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre
,NumeroTarjeta as Tarjeta
,tarjetas.cliente as Cliente
,Producto.nombre as Producto
,Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '4' and replace(cedula,'.','') = $v4 and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by RegistroConsultaSaldoSMS.FechaConsulta asc) T4
union all
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'-',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono
,Cedula AS Cedula
,CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre
,NumeroTarjeta as Tarjeta
,tarjetas.cliente as Cliente
,Producto.nombre as Producto
,Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '3' and replace(cedula,'.','') = $v4 and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by RegistroConsultaSaldoSMS.FechaConsulta asc) T5
union all
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'-',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono
,Cedula AS Cedula
,CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre
,NumeroTarjeta as Tarjeta
,tarjetas.cliente as Cliente
,Producto.nombre as Producto
,Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '2' and replace(cedula,'.','') = $v4 and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by FechaConsulta asc) T6
union all
Select * From
(Select top 10000000
CONCAT('0',SUBSTRING(NumeroTelefono,3,3),'-',SUBSTRING(NumeroTelefono,6,3),'-',SUBSTRING(NumeroTelefono,9,12)) AS NumeroTelefono
,Cedula AS Cedula
,CONCAT(TarjetahabienteCliente.PrimerNombre,' ',TarjetahabienteCliente.PrimerApellido) AS Nombre
,NumeroTarjeta as Tarjeta
,tarjetas.cliente as Cliente
,Producto.nombre as Producto
,Convert(varchar(10),FechaConsulta,103) as Fecha
from Operaciones.RegistroConsultaSaldoSMS
inner join tarjetas on tarjetas.Numero = RegistroConsultaSaldoSMS.NumeroTarjeta
inner join Operaciones.TarjetahabienteCliente on TarjetahabienteCliente.id = Tarjetas.TarjetahabienteId
inner join Producto on Producto.id = tarjetas.Producto
where LEN(Cedula) = '1' and replace(cedula,'.','') = $v4 and FechaConsulta between @Fecha_ini and (@Fecha_fin + 1)
order by RegistroConsultaSaldoSMS.FechaConsulta asc) T7
";
$ejecutar = sqlsrv_query($con,$consulta);
$i = 0;
$NumeroFilas = sqlsrv_has_rows($ejecutar);
if($NumeroFilas == 0)
{
echo('<script> alert("No existen registros con los datos solicitados"); window.history.back();</script>');
}
else
{
while($fila = sqlsrv_fetch_array($ejecutar))
{
$NumeroTelefono = $fila['NumeroTelefono'];
$Cedula = $fila['Cedula'];
$Nombre = $fila['Nombre'];
$NumeroTarjeta = $fila['Tarjeta'];
$Cliente = $fila['Cliente'];
$Producto = $fila['Producto'];
$FechaConsulta = $fila['Fecha'];
$i++;
echo("<tbody>
<td>$NumeroTelefono</td>
<td>$Cedula</td>
<td>$Nombre</td>
<td>$NumeroTarjeta</td>
<td>$Cliente</td>
<td>$Producto</td>
<td>$FechaConsulta</td>
</tbody>
"
);
}
}```
I managed to solve the problem, below the solution to my case I will use the same variables raised:
1. First I verified how the dates (their formats) are being captured with a
var_dump($v2)
:$v2 = "27/09/2019"
2. The variables that handled dates I split into 3 smaller strings, using the php function/method called
substr()
that splits a string into smaller strings:$v5 = substr($v2,-4); $v6 = substr($v2,3,-5); $v7 = substr($v2,0,-8);
These three variables get day, month and year and what I did was to order them in this way yearmonthday , without separating them with any character.
3.
DateTime
I pass these three variables to my type variables within my query:declare @Fecha_ini datetime set @Fecha_ini = '$v5$v6$v7'
In this way, the date enters the query with this format:
'20190927'
, I investigated that this way of presenting the data when they are of the typeDate/DateTime
in SQL SERVER is the ideal one, since it is a Standard and it will ignore the problems that you may have present with respect to the configured Local Machine/Server time zone.4. Finally, I optimized the queries and in the variable that saves the results of the Dates I placed the php function/method called
date_format()
:$FechaConsulta = date_format($fila['FechaConsulta'],'d/m/Y');