-4

For example, there is a TV program between 18:30 and 19:30. It's 19:15 now. How do I get the time between these two times as a percentage? I need to find% for progress bar.

enter image description here

Thuy Rayn
  • 33
  • 6
  • So are you asking how to do the math of percentages (not really a PHP question) or how to get the difference between two times? – Patrick Q Nov 12 '19 at 15:38
  • Does this answer your question? [How to get time difference in minutes in PHP](https://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php) – user2342558 Nov 12 '19 at 15:39
  • 1
    Note that the accepted answer on that question is actually rather poor. The more upvoted answers are better. (this doesn't take away from the fact that it could be selected as a possible duplicate) – Patrick Q Nov 12 '19 at 15:43
  • I need to find% for progress bar. @PatrickQ – Thuy Rayn Nov 12 '19 at 20:29
  • @ThuyRayn That doesn't answer my question. There are multiple steps involved. What _specifically_ is giving you trouble? And, importantly, _what have you tried_? – Patrick Q Nov 12 '19 at 20:33

2 Answers2

0

Use the DateTime class:

<?php

$start = new DateTime('2014-09-18 21:00');
$now = new DateTime('2014-09-18 21:09');
$end = new DateTime('2014-09-18 21:36');

$total = $start->diff($end);
$totalTime = $total->format('%i');
$total = $start->diff($now);
$elapsedTime = $total->format('%i');

$percent = $elapsedTime / ($totalTime / 100);

echo $percent;

Which will output :

25

Which you can see in action here https://3v4l.org/Kjn6W

Learn about DateTime here: https://www.php.net/manual/en/class.datetime.php

Learn about DateTimeInterval formats here: https://www.php.net/manual/en/dateinterval.format.php

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
-1

That was the result I was looking for, thanks for your help.

<?php 

date_default_timezone_set('Europe/Istanbul');

$baslangic  = strtotime("2019-11-13 00:10:00");
$bitis      = strtotime("2019-11-13 01:00:00");
$simdi      = time();


if ($simdi < $baslangic) {
    $percentage = 0;
} else if ($simdi > $bitis) {
    $percentage = 100;
}else {
    $percentage = ($baslangic - $simdi) * 100 / ($baslangic - $bitis);
}

echo round($percentage,2);


?>

Thuy Rayn
  • 33
  • 6