Pain12
1 week ago

Doing calculation in Js with php!?

Posted 1 week ago by Pain12

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.

Please sign in or create an account to participate in this conversation.