When viewing the following query:
$qry = "select
CONVERT(VARCHAR(20),fechaSalida,108) AS fechaSalida,
CONVERT(VARCHAR(20),fechaEntrada,108) AS fechaEntrada,
convert(varchar(10), ((DATEDIFF(SECOND, fechaEntrada, fechaSalida)%86400)/3600)) + ':'+
convert(varchar(10), (((DATEDIFF(SECOND, fechaEntrada, fechaSalida)%86400)%3600)/60)) as sumaHoras
from tblasistencia where idclave = '".$empno."' and
fechaEntrada BETWEEN CONVERT(DATETIME, '".$fechainicio." 00:00:00',103) and
CONVERT(DATETIME, '".$fechainicio." 23:59:59',103)";
One has the impression that you are drowning in a glass of water at first.
I will leave the following criteria for your consideration:
Write an SQL query whose results are destined for a programming language (PHP or other), in where you repeat again and again columns as fechaSalida
, fechaEntrada
applying tasks as conversions, calculations, formatting, etc, etc, what it does is unnecessarily recharging the database manager.
My proposal is very simple:
- Obtain from the database the data you need, without so many repetitions, calculations, conversions
- Treat data obtained by programming
If we apply this, your SQL query would look like this:
$qry = "SELECT
fechaSalida,
fechaEntrada,
FROM tblasistencia
WHERE ....
";
What you do here is simply bring the data as is to PHP and from there you can do the calculations you want.
PHP has the powerful class DateTime
which allows you to create a true date object from a well-represented date / time. With that class you just have to create two date objects and from them to do any type of operation, calculation, format, etc.
Suppose that fechaEntrada
and fechaSalida
show these values:
$fechaEntrada="2018-10-03 06:51:27";
$fechaSalida ="2018-10-03 19:51:00";
We create two objects from them:
$fechaIn=new DateTime($fechaEntrada);
$fechaOut=new DateTime($fechaSalida);
Well, both $fechaIn
and $fechaOut
are two objects with all of the law that represent your departure / arrival data.
If you want to see one of them inside:
var_dump($fechaIn);
Exit:
object(DateTime)#1 (3) {
["date"]=>
string(26) "2018-10-03 06:51:27.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/Berlin"
}
And what do I do with two objects, what does it do for me?
That from them you can do how many calculations and representations you want , expressed in hours, minutes, seconds, letters, days, weeks, quarters ...
Let's see some examples of representation (note that we always use the same object, not that we create the object again and again as it is done in your SQL query):
echo $fechaIn->format('Y-m-d H:i:s').PHP_EOL;
echo $fechaIn->format('Y-m-d').PHP_EOL;
echo $fechaIn->format('H:i:s').PHP_EOL;
echo $fechaIn->format('g:i A').PHP_EOL;
Exit:
2018-10-03 06:51:27
2018-10-03
06:51:27
6:51 AM
It's not just format, we can also do calculations . For example, I could not decipher the calculation you want to do here:
convert(varchar(10), ((DATEDIFF(SECOND, fechaEntrada, fechaSalida)%86400)/3600)) + ':'+
convert(varchar(10), (((DATEDIFF(SECOND, fechaEntrada, fechaSalida)%86400)%3600)/60)) as sumaHoras
but whatever the calculation you can do from your two objects and at the same time express it in a given format.
Suppose you want to know how many hours have passed between entry and exit:
$timeWorking = $fechaIn->diff($fechaOut);
What produces $timeWorking
is not just any variable, it is also an object with a quantity of information that you hallucinate. Let's see it inside:
object(DateInterval)#3 (15) {
["y"]=>
int(0)
["m"]=>
int(0)
["d"]=>
int(0)
["h"]=>
int(12)
["i"]=>
int(59)
["s"]=>
int(33)
["weekday"]=>
int(0)
["weekday_behavior"]=>
int(0)
["first_last_day_of"]=>
int(0)
["invert"]=>
int(0)
["days"]=>
int(0)
["special_type"]=>
int(0)
["special_amount"]=>
int(0)
["have_weekday_relative"]=>
int(0)
["have_special_relative"]=>
int(0)
}
Among the information that has $timeWorking
we have the number of years y
, months m
, days d
, hours h
.... that have elapsed between the two original objects and many more things .
And the thing does not end there, if you want to present that information in a certain way, you can use another method applied to that same object . For example:
echo $timeWorking->format('%H:%I:%S');
Exit:
12:59:33
Conclusion
I'm sorry if I've gone a bit longer in the explanation.
The answer was aimed at showing the power of the objects .
In cases like these it is convenient to charge as little as possible to the
database, bring our information in the simplest way
possible and then draw on the possibilities of language (in this
case class DateTime
of PHP) to manipulate that information.
I hope the answer is useful. What I propose is to focus the
solving the problem from another point, simplifying the code.
NOTE ON SECURITY:
The SQL query that you are launching from PHP is highly vulnerable to SQL injection attacks. Consider the use of queries prepared to neutralize that possibility of attack that could have unfortunate consequences on your data or on your system itself.