Calculate DateDiff in PHP in minutes [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
<?php
$sql_apr = " SELECT SUM ( meter * minute ) FROM table";
$rs_apr = #mysql_query($sql_apr);
$total_apr = #mysql_fetch_array($rs_apr);
$try4 = $total_apr['SUM(meter * minute'];
while ($rs_t = #mysql_fetch_array($rs_t)) {
$minute = '';
$sql_t = "SELECT DATEDIFF(MINUTE,'e_date e_time','s_date s_time') AS minute";
$rs_t = #mysql_query($sql_t);
$minute = $rs_t['minute'];
}
?>

You are looking at the wrong result:
$rs_t=#mysql_query($sql_t);
$minute = $total_t['minute'];
should be
$rs_t=#mysql_query($sql_t);
$minute = $rs_t['minute'];
// ^ use the rs_t result, not the result from the first query
You are also result the variable that you are looping on. I highly doubt that this while loop will ever end. You are looping on the result from $rs_t and then you reassign $rs_t inside the loop.

why don't you do this in correct order
$query = "SELECT DATEDIFF(MINUTE,'e_date e_time','s_date s_time') AS minute";
$results = #mysql_query($query);
$row = #mysql_fetch_array($results);
$minute = $row['minute'];
print_r($minute);
$sql_apr = " SELECT SUM ( meter * " . $minute . " ) AS my_sum FROM table";
$rs_apr = #mysql_query($sql_apr);
$total_apr = #mysql_fetch_array($rs_apr);
$try4 = $total_apr['my_sum'];
You try to get result (in while) before you run query.
edit:
$query = "SELECT TIMESTAMPDIFF(MINUTE,'s_date s_time','e_date e_time') AS minute";

Related

PHP; how do I sum a variable after $POST with issest [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a massive file with a few variable I would like to sum(add) after they are POST.
I should note that I am doing this in TCPDF. I don't know if it makes a difference. Nonetheless, The variables I am trying to sum are being posted via an isset function. After the variables are posted, I declared a new variable. Example $sum = $two+$three+$four+$five;. However, when I do this I get an undefined variable error. Ideally, I would like declare a new variable that takes the summed values and placed them in a HTML string in my TCPDF file. for example Total: $sum
//var_dump($_POST);
if (isset($_POST)) {
if(isset($_POST['two'])){ $two = $_POST['two'];}else{$two = " ";}
if(isset($_POST['three'])){ $three = $_POST['three'];}else{$three = " ";}
if(isset($_POST['four'])){ $four = $_POST['four'];}else{$four = " ";}
if(isset($_POST['five'])){ $five = $_POST['five'];}else{$five = " ";}
if(isset($_POST['six'])){ $six = $_POST['six'];}else{$six = " ";}
if(isset($_POST['seven'])){ $seven = $_POST['seven'];}else{$seven = " ";}
if(isset($_POST['eight'])){ $eight = $_POST['eight'];}else{$eight = " ";}
if(isset($_POST['nine'])){ $nine = $_POST['nine'];}else{$nine = " ";}
if(isset($_POST['ten'])){ $ten = $_POST['ten'];}else{$ten = " ";}
if(isset($_POST['elve'])){ $elve = $_POST['elve'];}else{$elve = " ";}
if(isset($_POST['twelv'])){ $twelv = $_POST['twelv'];}else{$twelv = " ";}
Maybe try one at at time. Its tedious but i think it might work.
$sum = $one + $two;
$sum = $sum +$three;`
`

Retrieve and compare time from database in php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hello I wasn't able to retrieve time data from database and display it on my site.
I think that my problem is in while loop or comparing time in if statement.
I'm positive that sql query is correct because I checked it.
Below is my code. Thantks for helping me.
$timeSql = "SELECT `Godzina_od`, `Godzina_do` FROM `godziny_przyjec` WHERE Id_dnia_przyjec = '$idDniaRow[Id_dnia]' and Id_uzytkownika = '$idLekarza'";
$timeResult = $connection->query($timeSql);
$timeRow = $timeResult->fetch_assoc();
$tStart = strtotime($timeRow['Godzina_od']);
$tEnd = strtotime($timeRow['Godzina_do']);
$getTimeResult = $connection->query("SELECT `Godzina` FROM `wizyty_lekarskie` WHERE Id_dnia ='$idDniaRow[Id_dnia]' and Id_lekarza_prowadzacego = '$idLekarza'");
while($tStart < $tEnd)
{
while($getTimeRow = $getTimeResult->fetch_assoc())
{
if(strtotime($getTimeRow['Godzina']) != $tStart)
{
echo '<option value="'.date("H:i", $tStart).'">'.date("H:i", $tStart).'</option>';
$tStart = strtotime('+20 minutes', $tStart);
}
else
{
$tStart = strtotime('+20 minutes', $tStart);
}
}
}
try using only one while loop remove the second while loop, and retrieve
$tStart = strtotime($timeRow['Godzina_od']);
$tEnd = strtotime($timeRow['Godzina_do']); as
$tStart = $timeRow['Godzina_od'];
$tEnd = $timeRow['Godzina_do']; then do you strotime outside that would work if you do this two things else contact me

How Generate data to alphabet on MySQL table using PHP? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Hello i have tables like this :
Employee
EmployeeID EmployeeName
1234 Sooyoung
1235 Yoona
1236 Tiffany
1237 Hyoyeon
1238 Taeyeon
1239 Seohyun
1240 Sunny
1241 Yuri
i want to generate the employee id sequentially like this :
1234 as A
1235 as B
1236 as C
1237 as D
1238 as C
1239 as E
1240 as D
1241 as F
may you know what mysql code and php what i have to use? thank you
This is how you would do it in PHP, I am assuming this is the method you want seeing as you tagged PHP.
<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("yourdatabase") or die(mysql_error());
$employees = mysql_query("SELECT * FROM Employee ORDER BY EmployeeID")
or die(mysql_error());
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$position = 0;
$position2 = 0;
$toomany = '';
while($row = mysql_fetch_array( $employees )) {
echo "<DIV>" . $toomany.substr($letters, $position, 1) . " = " . $row['EmployeeName'] . " </div>";
$position ++;
if($position > 25) {
$position = 0;
$position2 ++;
if($position2 > 25) { echo "We need to rethink this idea."; break; }
$toomany = substr($letters, $position2, 1);
}
}
?>
EDITED: What if you run out of alphabets ? what did you want to happen? I took a guess, but you will have to explain more if I am not right.
EDITED: TurdPile has a simplified version of what I had. Accept his answer.
As stated in the comments, I don't think you can work with data directly and evaluate it to the alphabet as you are wishing (of course you can still assign results to variables).
If you must have A-Z for whatever reason, I think the best solution would be to use a PHP script to parse the results of the query. If this is the case, you won't really need the Employee ID number, as you can work with the occurrence of the results, but I left the ID number in there anyways.
<?php
// DB connection info
include("mysql_connection.php");
$query = mysql_query("SELECT EmployeeID, EmployeeName FROM Employees WHERE EmployeeID >= 1234");
$results = mysql_fetch_array($query);
$iteration = "A";
echo "ID - Name";
while(list($empNumber,$empName) = $results)){
echo $iteration." - ".$empName;
++$iteration;
}
?>
Note: mysql_query was used for the example, generally you should use MySQLi or PDO.
Note 2: The use of $iteration in this will automatically increment the letter output, when it reaches Z, it should go onto AA.

Apply sorting algorithm to database query [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I want to make another list with "What's Hot" like reddit.
I found this topic where it explains how their sorting algorithm works
First I want to ask, if it is legal to use their algorithm?
And if yes, how would I apply it to PHP database query.
Do I need to SELECT all posts first and then sort it?
function hot($ups, $downs, $date) {
$s = $ups - $downs;
$order = log(max(abs(s), 1), 10);
if(s > 0) {
$sign = 1;
} elseif(s < 0) {
$sign = -1;
} else {
$sign = 0;
}
$date = new DateTime($date);
$seconds = $date->format('U');
return round($order + $sign * $seconds / 45000, 7);
}
this is what I get when I convert it to PHP.
Assuming your ups and downs columns are called ups and downs, then something like:
ORDER BY ROUND(
( LOG10(
GREATEST(
ABS(`ups` - `downs`),
1
)
) +
SIGN(`ups` - `downs`) *
UNIX_TIMESTAMP(`date_posted`) / 45000
),
7
)
It might be a better idea to use this formula to create a calculated column in your select list, and then order by that column
EDIT
Example of a calculated column:
SELECT `ups`,
`downs`,
`posted_date`,
ROUND(
( LOG10(
GREATEST(
ABS(`ups` - `downs`),
1
)
) +
SIGN(`ups` - `downs`) *
UNIX_TIMESTAMP(`date_posted`) / 45000
),
7
) AS hotness
FROM `posts`
So hotness is your calculated column

edit column in pdo select [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a column that is in seconds and I need to format it in Minutes:seconds, I am thinking that I will need to loop through the fetched array but not quite sure on how to go about that.
$query = 'SELECT calldate,recordingfile,uniqueid,clid,did,lastapp,dst,disposition,duration FROM cdr';
$sql = $remotedbh->prepare($query);
$sql->execute();
$dblist = $sql->fetchAll(PDO::FETCH_ASSOC);
while($row -)
if($sql->rowCount() > 0){
header('Content-type: application/json');
echo json_encode($dblist);
}
else {
echo 0;
}
?>
I would suggest changing the value in the query itself. Here is an example
SELECT calldate,
recordingfile,
uniqueid,
clid,
did,
lastapp,
dst,
disposition,
duration,
CONCAT(FLOOR(duration/60),':',LPAD((duration % 60), 2, '0')) AS duration_in_mins_and_secs
FROM cdr
Here I am assuming duration is the field you want to modify. I am simply dividing the duration by 60 to get minute component and then concatenating the remainder to it.
If you need this data regularly (i.e. you are going to perform this query a lot), it would probably be best to actually store this calculated field in the records themselves so you don't need to make a calculation at all when querying the data. Simply make this calculation upon each record insert.
I suggest fetching one row at a time rather than fetching them all into an array:
while ($dbrow = $sql->fetch(PDO::FETCH_ASSOC)) {
// process current row
}
If you'd rather fetch them all into one array, you can use PHP's foreach:
$dblist = $sql->fetchAll(PDO::FETCH_ASSOC);
foreach ($dblist as $dbrow) {
// process current row
}
The following code will format the duration in M:S format:
while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
$dur = $row["duration"];
$min = $sec = 0;
if (floor($dur / 60) > 0) {
$min = floor($dur / 60);
$sec = $dur % 60;
}
else {
$sec = $dur;
}
$str = $min . ":" . $sec;
}
However, I recommend having your database format the output for you, as Mike Brant specifies in his answer.

Categories