Itz Raghu
Itz Raghu

Reputation: 457

Comparing current datetime with datetime from database without refreshing page

I want to make a simple reminder which displays something when the current date and time matches an entry in a database, without refreshing the page.

I have two entries in my database in a table called date:

The page reminder.php fetches the date and time stored in database and converts it into Timestamp.

This page also converts the current date and time into Timestamp using strtotime("now").

It displays both date and time when both values match by continuously refreshing the page every second.

I want to compare both values without refreshing the reminder.php page.

reminder.php

<?php
require_once 'php/database.php';
date_default_timezone_set('Asia/Kolkata');
$current = strtotime('now');
$stmt = $db->query("SELECT * FROM date");
$row = $stmt->fetchall(PDO::FETCH_ASSOC);
foreach ($row as $key) {
    $dateTime = $key['date'] . $key['time'];
    $dateTime  = strtotime($dateTime);

}

if ($dateTime == $current) {
    echo  "both date and time are same";
}
else {

    echo "both date and time are not same";

}

Upvotes: 0

Views: 977

Answers (1)

Vishnu R Nair
Vishnu R Nair

Reputation: 317

If I understands right then you need to check date from the database and current date without refreshing the page use this code , HTML Part :

<button id="data_pass" name="btn" >BUTTON</button>

JQuERy :

 $(document).ready(function() {
$("#data_pass").click(function() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
   dd='0'+dd
} 

if(mm<10) {
  mm='0'+mm
} 

today = yyyy+'-'+mm+'-'+dd;

$.ajax({
url  : 'test.php',
type : 'post',
data : {"today":today},
success : function(data) {
    alert(data);                                        
},
error : function(data) {
    alert("error");
}


});

});
});

PHP Part :

  include('db_con.php');

   class date_check extends db_connection { 
   function dates() {
    $con    = $this->db_con();
    $sel    = $con->prepare("select * from dates");
    $exe    = $sel->execute();
    $dates  = $_POST['today'];
    foreach ($sel as $select) {
        if ($select['date_i'] == $dates) {
            echo "Happy BirthDay";  
        }   
    }
}


}

 $obj   = new date_check;
$obj->dates();

It's only for checking the date. hope it will help you

Upvotes: 0

Related Questions