0

im using PHP

I don't know if this is possible but i have a start date and and end date, im wanting to get the percentage of completed time from these two dates.

so if it will need to take the start end and current time and date and fire back

35% Completed etc..

Nov 02nd 2015 14:05:15 Nov 02nd 2015 18:05:15

Thanks any tips or guide is a big help.

I have been trying to use this but its not working.

<?php

$startDate = date_create('$start');
$endDate = date_create('$end');
$currentDate = date_create('$date');

$totalTime = date_diff($endDate, $startDate); 
$elapsedTime = date_diff($currentDate, $startDate);

$totalTimeDays = $totalTime->format("%d");
$elapsedTimeDays = $elapsedTime->format("%d");

echo "Total project time = " . $totalTimeDays . "<br/>";
echo "Elapsed project time = " . $elapsedTimeDays  . "<br/>";
echo "Percent of project complete = " . ($elapsedTimeDays / $totalTimeDays) * 100.0;

?>
Liam Armour
  • 119
  • 1
  • 9

1 Answers1

0

You should remove your quotes on the first 3 lines

$startDate = date_create($start);
$endDate = date_create($end);
$currentDate = date_create($date);

And the format you use is wrong => use the %a which count all days

--EDIT WITH COMPLETE EXAMPLE--

<?php
// Note that i prefer the use of true object notation
$start = '2010-01-01';
$end = '2010-10-01';
$date = '2010-05-01';

$startDate = new DateTime($start);
$endDate = new DateTime($end);
$currentDate = new DateTime($date);

$totalTime = $endDate->diff($startDate); 
$elapsedTime = $currentDate->diff($startDate);

$totalTimeDays = $totalTime->format('%a'); // the change is here
$elapsedTimeDays = $elapsedTime->format('%a');  // the change is here
//var_dump($totalTime, $elapsedTime);

echo "Total project time = " . $totalTimeDays . "<br/>";
echo "Elapsed project time = " . $elapsedTimeDays  . "<br/>";
echo "Percent of project complete = " . ($elapsedTimeDays / $totalTimeDays) * 100.0;

?>

-- EDIT 2--- Your 500 error is a PHP division by 0 error as the $elapsedTime->format('%a'); returns 0 (or NULL)

-- EDIT 3 --

$start = '2015-11-02 14:05:15';
$end = '2015-11-02 18:05:15';
$date = '2015-11-02 16:12:15';

$startDate = new DateTime($start);
$endDate = new DateTime($end);
$currentDate = new DateTime($date);


$totalTime = $endDate->getTimestamp() - $startDate->getTimestamp();
$elapsedTime = $currentDate->getTimestamp() - $startDate->getTimestamp();

echo "Total project time = " . $totalTime . "<br/>";
echo "Elapsed project time = " . $elapsedTime  . "<br/>";
echo "Percent of project complete = " . ($elapsedTime / $totalTime) * 100.0;
EmmanuelC
  • 136
  • 9