Search code examples
laravelphp-carbon

How do you get percentage of time left for carbon php? (Laravel 8.X)


So I have the following:

$timeLeft   = $queue->completed_at->diffInSeconds(now()); 

dd($timeLeft);

Where $queue is a model (Laravel) with the attribute of completed_at (datetime).

What I am trying to do is essentially: now() / $queue->completed_at as a percentage of time left till it's done. (ie, 0.75)

I have seen other solutions where they take now - end time / start - end (or something similar)

Can I not accomplish the same thing with now() and completed_at ?


Solution

  • Can I not accomplish the same thing with now() and completed_at ?

    No, you need a start-time to have a percentage.

    In this example, now() minus start will give you the amount of time elapsed, which can be compared to the entire time to give you a percentage.

    [==================|=========]
    start              now       completed_at
    

    Here, however, there's no percentage to calculate. You only have now() and completed_at.

    |=============]
    now           completed_at