jquery - Time To Percent Using PHP -
this may sound dumb question, how can convert time between 2 dates percent?
i using jquery plugin: http://tinacious.github.io/goalprogress/
the script on page calculates percent is:
$('#timergoal').goalprogress({ goalamount: 100, currentamount: 40, textbefore: '', textafter: '% completed.' });
where says goalamount:
i'd remain @ 100
, says currentamount: 40,
i'd somehow find difference in percentage between 2 days, know i'd have set start date, current date, , end date find percentage.
i'm part of code have be:
$startdate = '01/01/2015 12:00:00'; $currentdate = date('d/m/y h:i:s'); $enddate = '02/15/2015 12:00:00';
finding difference in 2 dates easy, it's third date thing cannot grasp, make percentage.
any ideas?
i thinking along lines of:
[taken from: how find difference in days between 2 dates ]
$daylen = 60*60*24; $date1 = '2010-03-29'; $date2 = '2009-07-16'; echo (strtotime($date1)-strtotime($date2))/$daylen;
but read on 2 dates not three.
here i've come with. it's not calculating percentages yet, it's 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
getting 901.2015119993
not percent, it's 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 working code , todays date being 08/07/2015
value of $datepercent
14
.
the difference between 2 times, itself, can't converted percentage. it's period of time. in order figure out percentage complete, need know how long entire goal supposed take (an estimated time, assume.) can figure out percentage this:
elapsedtime / totaltime * 100
the total time end date - start date
, , elapsed time now - start date
.
rather using string functions manipulate dates, better 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 $enddate = new datetime($enddate); $totaltime = $enddate->diff($startdate)->format('%a'); $elapsedtime = $currentdate->diff($startdate)->format('%a'); // diff returns dateinterval object; calling format method // %a returns number of days in interval $percent = ($elapsedtime / $totaltime) * 100;
Comments
Post a Comment