Retrieve and compare time from database in php [closed] - php

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

Related

How can i redirect my visitors to different different urls like say after every 10 mins or 15 mins vica-versa? [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.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I need a php code which i upload on my domain. what it will do is redirect visitors to different different urls based on certain time period like say for the first 10 mins it should redirect to google.com later on for the next 10 mins to gmail or something else whatever which i specify and so on. Is there is any way to do that coz right now i have to do it manually which is really hectic. All i got so far is this but its not working!
`
$tym = date("h:i");
echo $tym;
if ($tym >= "06:10" && $tym < "06:20"){
$urls = array(
array('url' => 'http://website1.com'),
);}
if ($tym >= "06:20" && $tym < "06:30"){
$urls = array(
array('url' => 'http://website2.com'),
);}
//and so on
$probs = array();
foreach ($urls as $i => $url) {
$probs = array_merge($probs, array_fill(1, $i));
}
$rand = array_rand($probs);
var_dump($rand);
var_dump($urls[$probs[$rand]]['url']);
header('Location: '.$urls[$probs[$rand]]['url']);`
Need some help please! Thanks in advance.
I have corrected a few things and now it's all working. Here's the correct part:
date_default_timezone_set('Asia/Kolkata'); //your default timezone is utc
$tym = date("h:i");
if ($tym >= "06:00" && $tym < "07:00"){ //every hour values can be added similarly
$urls = array(
array('url' => 'https://www.google.com/', 'weight' => 100) //url to specify
);}
$probs = array();
foreach ($urls as $i => $url) {
$probs = array_merge($probs, array_fill(1, $url['weight'], $i));
}
$rand = array_rand($probs);
var_dump($rand);
var_dump($urls[$probs[$rand]]['url']);
header('Location: '.$urls[$probs[$rand]]['url']);

Formatting month names as alphabets [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to create an associative array that will parse an alphabet for each month name. e.g JAN - A, FEB - B, MAR - C... I need some help for this. I have done a lot of searching but not able to. Can anyone help please?
Thanks in advance.
<?php
$my_month = date('m');
$months = array("JAN"=>"A",
"FEB"=>"B",
"MAR"=>"C",
"APR"=>"D",
"MAY"=>"E",
"JUN"=>"F",
"JUL"=>"G",
"AUG"=>"H",
"SEP"=>"I",
"OCT"=>"J",
"NOV"=>"K",
"DEC"=>"L" );
if ($my_month == date('M')) {
echo $months[$my_month];
} else {
echo 'almost there';
}
?>
The code you need to create an array as you want is this:
$months['1'] = 'A';
$months['2'] = 'B';
$months['3'] = 'C';
$months['4'] = 'D';
$months['5'] = 'E';
$months['6'] = 'F';
$months['7'] = 'G';
$months['8'] = 'H';
$months['9'] = 'I';
$months['10'] = 'J';
$months['11'] = 'K';
$months['12'] = 'L';
Or even better:
$months = array("1"=>"A",
"2"=>"B",
"3"=>"C",
"4"=>"D",
"5"=>"E",
"6"=>"F",
"7"=>"G",
"8"=>"H",
"9"=>"I",
"10"=>"J",
"11"=>"K",
"12"=>"L" );
And let's say the $my_month variable has the month (in the form 'JAN', 'FEB', ...) from which you want to get the corresponding letter. This is what you write to get the letter:
echo $months[$my_month];

What's wrong with mt_rand 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 7 years ago.
Improve this question
I tried to make a simple name generator. Help me to identify why it's not working. I think it's because of mt_rand function. Sorry if question seems banal or irrelevant, I'm first time here and new to programming. Here's the code:
<?php
echo 'Ovo je moja verzija Polumenta generatora';
$prvo = 'bcdfghtnjpknmlrjdzdjs';
$drugo = 'aeiou';
$trece = 'bcdfghtnjpknmlrjsdzdj';
$cetvrto = 'uo';
$prvos = mt_rand($prvo[0],$prvo[17]);
$drugos = mt_rand($drugo[0],$drugo[4]);
$treces = mt_rand($trece[0],$trece[17]);
$cetvrtos = mt_rand($cetvrto[0],$cetvrto[1]);
echo $prvos.$drugos.$treces.$cetvrtos.' Polumenta'
?>
mt_rand takes integers as arguments and returns an integer. You are trying to pass it characters and return characters. You should do something like:
<?php
echo 'Ovo je moja verzija Polumenta generatora';
$prvo = 'bcdfghtnjpknmlrjdzdjs';
$drugo = 'aeiou';
$trece = 'bcdfghtnjpknmlrjsdzdj';
$cetvrto = 'uo';
$prvos = $prvo[mt_rand(0,strlen($prvo) - 1)];
$drugos = $drugo[mt_rand(0,strlen($drugo) - 1)];
$treces = $trece[mt_rand(0,strlen($trece) - 1)];
$cetvrtos = $cetvrto[mt_rand(0,strlen($cetvrto) - 1)];
echo $prvos.$drugos.$treces.$cetvrtos.' Polumenta'
?>
You can make it in another way, using arrays and rand() function.
$chrs[0] = str_split('bcdfghtnjpknmlrjdzdjs'); // array of consonants
$chrs[1] = str_split('aeiou'); // array of vowels
$length = 8; // nick name length
Than generate random sequence of chars any length
for($i = 0; $i < $length; $i++)
{
$v_or_c = rand(0,1);
if($v_or_c)
{
$nick_name .= $chrs[$v_or_c][rand(0, sizeof($chrs[$v_or_c]))];
}
else
{
$nick_name .= $chrs[$v_or_c][rand(0, sizeof($chrs[$v_or_c]))];
}
}
echo ucfirst($nick_name); // ucfirst - to upper case first letter

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.

Calculate DateDiff in PHP in minutes [closed]

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";

Categories