Difference Between Database Date and Today’s Date Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Difference Between Database Date and Today’s Date without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am developing a website that allow user to see their equipments and know how much days passed till the repair started.

But i am having trouble getting the DATABASE information about the date. I registered a column DATE and i made it TIMESTAMP with CURRENT_TIMESTAMP so it can automaticly atribut the date that joined the system.

I am using a script to get today’s date and i put it in a table so it can show the user:

<script>
var days = new Date(); 
document.getElementById("days").innerHTML = days.toISOString().substr(0, 10); 
</script>

Then i called a function that gets all equipment info ( client name, DATE, Type, etc…):

include_once('DataAccess.php');
$da = new DataAccess();
$res = $da->getEquipmentInfo();
while($row = mysqli_fetch_object($res)){
echo " <td id='days'></td> "; }

How do i make to calculate the difference between the database columns DATE and today’s date to know how much days passed?

Thank you.

Answer

Convert both dates into unix and then find the difference between them and depending on if you want days, weeks, minutes, ect. you will have to convert. I dont have enough information for on your code and all your javascript to do it for you but here is a php example.

<?php
   $now = time(); 
   $db_unix = strtotime($res->theMysqlTimestamp);
   $datediff = $now - $db_unix;
   echo floor($datediff/(60*60*24));
?>

This takes the date now, your timestamp from mysql gets the unix timestamps, subtracts them, and then calculates the days difference between them.

We are here to answer your question about Difference Between Database Date and Today’s Date - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji