Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ziben69's avatar

How to convert Date fields to timestamps and substract them?

Hello guys,

I have question, maybe you can help me.

I have two fields in table Task:

startDate, stopDate

How can i convert to the timestamps and substract them. I need one field with, something like: deadline field?

I have MVC standard in my project. So much thanks :*

0 likes
7 replies
ziben69's avatar

Yep i am using Eloquent. I have view of list tasks with task details:

<div class="container">
    <div class="card">
        <div class="card-header">Task{{ $task->id }}</div>
        <div class="card-body">
            <table class="table">
                <tr>
                    <td>Name</td>
                    <td>{{ $task->name }}</td>
                </tr>
                <tr>
                    <td>Progress</td>
                    <div class="progress">
                        <div id="dynamic"
                            class="progress-bar progress-bar-success progress-bar-striped active"
                            role="progressbar" aria-valuenow="0" aria-valuemin="0"
                            aria-valuemax="100" style="width: 0%">
                            <span id="current-progress"></span>
                        </div>
                    </div>
                    <td>{{ $task->stopDate }}</td>
                </tr>
                <tr>
                    <td>Description</td>
                    <td>{{ $task->description }}</td>
                </tr>
            </table>
        </div>
    </div>
</div>

and i will do progress bar where his step is calculated from the difference in dates, but I have no idea how to do it.

I dont know JavaScript, i know that i should get Date values, and substract them, but need help.

Tomi's avatar
Tomi
Best Answer
Level 8

Well you could calculate this in the backend:

{{$task->startDate->diffInRealHours($task->stopDate)}} this could be your valuenow

not sure what you max value is.

this would return this difference in hours.

Also if you want to do something on the frontend with dynamically updating your blade you might want to look into Moment.js

https://momentjs.com/

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b) // 86400000
ziben69's avatar

i have JS file:

window.onload = (function() {
  var current_progress = 0;
  
  var interval = setInterval(function() {
      current_progress += 10;
      $("#dynamic")
      .css("width", current_progress + "%")
      .attr("aria-valuenow", current_progress)
      .text(current_progress + "% Complete");
      if (current_progress >= 100)
          clearInterval(interval);
  }, 1000);
});

but its only example, it work. Dont know how to pass eloquent to javascript file

Tomi's avatar

var task = {{ $task }}

or if its an array you can also:

  var task = @json($task);
m7vm7v's avatar

@ziben69 Could you please click on the best-reply icon under the @Tomi 's avatar. He deserves the badge after his answer is the one that helped you.

1 like

Please or to participate in this conversation.