I am trying to do a silly QPI function where I'd take a set of recorded times stored in a SQL database, subtract them from one another and then divide them by the number of tasks (or rows) I have selected in the SQL query.
The problem I am having is that the periods of time I have are subtracted (de facto its a duration between two timestamps) and are in the format 0000-00-00 00:38:11
being the subtraction between 2019-04-19 05:59:36.000
and 2019-04-19 06:37:47.000
.
If I strtotime('0000-00-00 00:38:11')
it would return a value that is 943922291
which my fairly accurate function to convert seconds to dates turns it into 29 years, 10 months, 29 days, 38 minutes and 11 seconds
which is obviously incorrect.
I thought about doing most of the things more programmatically and to be less dependent on libraries. I 'exploded' the string into its components and did some basic (and wrong multiplications as it disregards leap years). But this is not the right way to do it, I am sure of it.
Is there a way to convert the duration into seconds using the system libraries?
Here is my silly string date to time converter, just in case something useful might come out of it:
function myFunc($input_date)
{
$date = explode(" ", $input_date);
//////////////////////////////
$yearmonthday = explode("-", $date[0]);
//////////////////////////////
$years=$yearmonthday[0];
$months=$yearmonthday[1];
$days=$yearmonthday[2];
/////////////////////////////
$hoursminutesseconds = explode(":", $date[1]);
$hours=$hoursminutesseconds[0];
$minutes=$hoursminutesseconds[1];
$seconds=$hoursminutesseconds[2];
echo '---------------<br>';
echo $input_date;
echo '<br>---------------<br>';
echo "years: ".$years." months: ".$months." days: ".$days." hours: ".$hours." minutes: ".$minutes." seconds: ".$seconds;
echo '<br>---------------<br>';
echo 'MINUTES: '.$minutes.'<br>';
$minsToSec = $minutes * 60;
echo 'Mins to Sec: '.$minsToSec.'<br>';
echo 'HOURS: '.$hours.'<br>';
$hoursToSec = $hours * 3600;
echo 'Hours to Sec: '.$hoursToSec.'<br>';
echo 'DAYS: '.$days.'<br>';
$daysToSec = $days * 86400;
echo 'Days to Sec: '.$daysToSec.'<br>';
echo 'Months: '.$months.'<br>';
$monthsToSec = $months * 2629746;
echo 'Months to Sec: '.$monthsToSec.'<br>';
echo 'Years: '.$years.'<br>';
$yearssToSec = $years * 31556952;
echo 'Years to Sec: '.$yearssToSec.'<br>';
$combined = $minsToSec + $daysToSec + $monthsToSec + $yearssToSec;
//echo 'All combined: ' . $combined. '<br>';
return $combined;
}
Below is the aforementioned seconds to date converter:
function convertSecToTime($sec)
{
$date1 = new DateTime("@0");
$date2 = new DateTime("@$sec");
$interval = date_diff($date1, $date2);
$parts = ['years' => 'y', 'months' => 'm', 'days' => 'd', 'hours' => 'h', 'minutes' => 'i', 'seconds' => 's'];
$formatted = [];
foreach($parts as $i => $part)
{
$value = $interval->$part;
if ($value !== 0)
{
if ($value == 1){
$i = substr($i, 0, -1);
}
$formatted[] = "$value $i";
}
}
if (count($formatted) == 1)
{
return $formatted[0];
}
else
{
$str = implode(', ', array_slice($formatted, 0, -1));
$str.= ' and ' . $formatted[count($formatted) - 1];
return $str;
}
}