1

This may sound like a dumb question, but how can I convert the time between two dates to a percent?

I am using this Jquery plugin: http://tinacious.github.io/goalProgress/

The script on page that calculates the percent is:

$('#timerGoal').goalProgress({
                    goalAmount: 100,
                    currentAmount: 40,
                    textBefore: '',
                    textAfter: '% Completed.'
                });

Where it says goalAmount: I'd like that to remain at 100, but where it says currentAmount: 40, I'd somehow like to find the difference in percentage between two days, I know I'd have to set a start date, current date, and end date to find a percentage.

I'm certain part of the code would have to be:

$startDate = '01/01/2015 12:00:00';
$currentDate = date('d/M/Y H:i:s');
$endDate = '02/15/2015 12:00:00';

Finding the difference in two dates is fairly easy, but it's the third date thing I cannot grasp, especially to make it a percentage.

Any ideas?

I was thinking something along the lines of:

[Taken from: How to find the difference in days between two dates ]

$daylen = 60*60*24;

   $date1 = '2010-03-29';
   $date2 = '2009-07-16';

   echo (strtotime($date1)-strtotime($date2))/$daylen;

But everything I read on is two dates not three.

Here is what I've come up with. It's not calculating percentages yet, but it's something to possibly go off of:

$startDate = '08/01/2015 12:00:00';
$currentDate = date('d/M/Y H:i:s');
$endDate = '09/01/2015 12:00:00';

$startDate =str_replace(array(':', '/', ' '), '', $startDate);
$currentDate =str_replace(array(':', '/', ' '), '', $currentDate);
$endDate =str_replace(array(':', '/', ''), ' ', $endDate);

$mainPercent = $endDate - $startDate;

$actualPercent = $endDate - $currentDate;

$displayPercent =  $actualPercent/$mainPercent * 100;

echo $displayPercent;

With todays date being 08/07/2015 I am getting 901.2015119993 which is obviously not a percent, but it's a start.

Working Solution:

$startDate = strtotime('08/01/2015 12:00:00');
$currentDate = time(date('d/M/Y H:i:s'));
$endDate = strtotime('09/15/2015 12:00:00');

$dateDivideBy = $endDate - $startDate;
$dateDivide = $currentDate - $startDate;

$divideProduct = $dateDivide / $dateDivideBy;

$datePercent = round($divideProduct * 100);

echo $datePercent;

With this working code and todays date being 08/07/2015 the value of $datePercent is 14.

Community
  • 1
  • 1
Jesse Elser
  • 974
  • 2
  • 11
  • 39
  • what's the point of `date('01/01/2015...')`? None of those are formatting characters, so you might as well just have `$start = '01/01/...'` and if you want to find the diff between time strings, you should `strtotime()` them into integers, on which you CAN do math. – Marc B Aug 07 '15 at 20:03

2 Answers2

2

The difference between two times, by itself, really can't be converted to a percentage. It's just a period of time. In order to figure out what percentage is complete, you would need to know how long the entire goal is supposed to take (an estimated time, I assume.) Then you can figure out the percentage like this:

ElapsedTime / TotalTime * 100

The total time would be End Date - Start Date, and the elapsed time would be now - start date.

Rather than using string functions to manipulate the dates, it would be better to use DateTime functions.

$startDate = '08/01/2015 12:00:00';
$endDate = '09/01/2015 12:00:00';

$startDate = new DateTime($startDate);
$currentDate = new DateTime(); // defaults to now
$endDate = new DateTime($endDate);

$totalTime = $endDate->diff($startDate)->format('%a');
$elapsedTime = $currentDate->diff($startDate)->format('%a');
// diff returns a DateInterval object; calling its format method 
// with %a returns the number of days in the interval

$percent = ($elapsedTime / $totalTime) * 100;
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • That's why I was thinking percentage using 3 times. My idea is an established start time and end time being 100% and then the current time to end time being the other percentage. I'm going to do some experimenting and see what I can come up with. – Jesse Elser Aug 07 '15 at 20:16
  • Looks like we are thinking along the same lines now :) I was confused at first, but I think I can figure this out. Thanks for jogging my brain into thinking. I will leave this up so when I find a solution I can post an answer for any possible future visits. – Jesse Elser Aug 07 '15 at 20:20
  • Thanks. Here is something I think I can work with, but cannot think of how to do. Let's say I have the date `01/01/2015 12:00:00` how can I convert that to a number like `01012015120000`? I think I can come up with a decent code if I can convert all the dates to just numbers. – Jesse Elser Aug 07 '15 at 20:22
  • 1
    JavaScript `Date` objects are just a representations of milliseconds, you can add and subtract them as you'd like without converting them. – Gnarlywhale Aug 07 '15 at 20:24
  • I've posted in my question the code I have written so far. I'm not getting percentages yet, but it's a start. Any suggestions based off of it? – Jesse Elser Aug 07 '15 at 20:51
  • @JesseElser I added an example of how to calculate this using PHP DateTime objects. – Don't Panic Aug 07 '15 at 21:32
  • I like it :) Sadly I just got it solved and was just about to post my solution :) Yours works so I will mark it as accepted, but I will add mine into my question if you want to take a look at the other possible one that works. – Jesse Elser Aug 07 '15 at 21:39
0

I believe this is your desired outcome, where result is the resulting percent difference between start_actual_time and percent_time:

var percent_time= new Date("01/17/2013 11:20");
var start_actual_time = new Date("01/17/2012 11:20");
var end_actual_time = new Date("01/17/2014 11:20");


var range = end_actual_time - start_actual_time;
var diff = end_actual_time - percent_time;

var result = (diff / range)*100;

In this example, start_actual_time and percent_time are 40% different.

Gnarlywhale
  • 4,030
  • 2
  • 15
  • 18