Just get the fields then calculate.
A simple example:
https://stackoverflow.com/questions/6462772/sum-of-two-textfields-javascript
I would suggest taking some javascript tutorials. Or jquery, or Vue, etc
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hey guys, I am trying to work with some data from the database or php in javascript.
What I tried in my blade.php
<?php
$timenow = date('H:m:s');
$timefrom = $post->timefrom;
$timeto = $post->timeto;
?>
<script>
var time = "<?php echo $timenow ?>"; // var time = "18:03:36";
var timefrom = "<?php echo $timefrom ?>"; // var timefrom = "04:04:00";
var timeto = "<?php echo $timeto ?>"; // var timeto = "23:03:00";
var timedif = "<?php echo $timeto ?> - <?php echo $timenow ?>"; // var timedif = "23:03:00 - 18:03:36"
$("#{{$post->id}}").click(function(){
alert(timedif); // alert("23:03:00 - 18:03:36")
});
</script>
I first get the actual time in php since its way easier to do. The goal is to calculate a time difference. What I don't know is how to tell js to take the difference between the two times. Since for Js everything is a string.
What I tried is to convert the actual time and the time from the DB to an integer in PHP. And then calculate with it in js.
I do all that to tell jquery later that if a timeslot is met it should add or remove certain elements from a div.
you can parse the datetime like so
let timeto = new Date(Date.parse("2020/3/26 <?php $timeto ?>"))
let timenow = new Date(Date.parse("2020/3/26 <?php $timenow ?>"))
$("#{{$post->id}}").click(function(){
alert(timeto.getHours() - timenow.getHours())
})
Please or to participate in this conversation.