-2
Good morning friends!
My goal is to compare two dates in PHP.
I want to check if $data1 is 24 hours longer than $data2. How do I do this?
Thank you!
-2
Good morning friends!
My goal is to compare two dates in PHP.
I want to check if $data1 is 24 hours longer than $data2. How do I do this?
Thank you!
0
Good morning Igor!
This will only work if you have BD! This program does not work on PDO (Object Oriented PHP)!
NOTE: At the end I put a if
that will compare the two values. There are several ways to do. I put this because I thought it was the simplest to understand.
Try doing it this way:
<?php $data1 = "" \\colocar o valor da data1 $data2 = "" \\colocar o valor da data2 $sql = "SELECT DATE_ADD(".$data1.", INTERVAL +1 DAY) AS data1_edit FROM tabela"; \\faz a consulta na BD $consulta = mysqli_query($connect, $sql); \\faz a conexão com a BD através da variável $connect $result = mysqli_fetch_assoc($consulta); $data1_edit = $result['data1_edit']; \\vai buscar a variável data1_edit ao SELECT feito em cima if ($data1_edit == $data2) {} \\serve para comparar as duas datas ?>
Good luck!
Thank you very much, dear! helped me too much! perfectly what I needed.
-1
Another suggestion would be that:
You can use with PDO:
<?php
class Horas_DAO {
private $conn = null;
public function verificaDatas($data1, $data2) {
require_once "conexao_pdo.php";
$retorno = 0;
$this->connection = Connection::getConn();
$query = "SELECT TIMESTAMPDIFF(HOUR, :data1, :data2) horas";
$stmt->bindValue( ':data1', $data1, PDO::PARAM_STR );
$stmt->bindValue( ':data2', $data2, PDO::PARAM_STR );
$stmt->execute();
try{
if ($row = $stmt->fetch( PDO::FETCH_ASSOC )) {
$retorno = $row['horas'];
}
}catch( PDOException $e ){
echo "Problema: ".$e->getMessage();
exit();
}
return $retorno;
}
}
?>
Then you would need to call the class method:
<?php
$ad = new Horas_DAO();
$r = $ad->verificaDatas('2019-05-01 10:00:00', '2019-05-01 10:05:00');
if( $r > 24 ){
// faz algo
}
?>
Why use the database to calculate the difference if PHP already does this?
It’s just an option
Which makes no sense anyone use. Is there any advantage?
It’s true, you’re right.
Browser other questions tagged php mysql
You are not signed in. Login or sign up in order to post.
Good morning Igor, you can explain yourself better?
– Leadnnd Pereira
Good morning, Leadnnd. Imagine two date variables, I want to compare them and know if the first date is 24 hours longer than the second, how do I do that?
– Igor Souza
According to your comment, to know the difference between minutes is like this:
SELECT TIMESTAMPDIFF(MINUTE, '2019-05-01 10:00:00', '2019-05-01 10:05:00') ;
– adventistaam
Do as @adventistaam said!
– Leadnnd Pereira
No, Anderson! My problem is different.
– Igor Souza
@Igorsouza What are the differences? You asked to compare dates, the question cited compares dates. To me it seemed the same problem - or maybe your question is not clear.
– Woss
Or you can see this response here, I think it takes what you want
– adventistaam