Saracoglu Saracoglu - 2 months ago 12
PHP Question

Page Generation Time for Human-FriendlyFormat

function PageStartTime() {
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
}

function pageFinishTime() {
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 1);
echo "Page generated in $total_time seconds.";
echo 'Peak memory usage: ',round(memory_get_peak_usage()/1048576, 2), 'MB';
}


Print:


Page generated in 1473241297.4 seconds.Peak memory usage: 0.56MB


How I can change
"human-friendly"
format?

Answer

You can do it like below:-

<?php
  error_reporting(E_ALL); //check all type of errors
  ini_set('display_errors',1); // display those if any happen
  $start = microtime();

  function PageStartTime($start) {
    $time = microtime();
    $time = explode(' ', $time);
    $time = $time[1] + $time[0];
    $start = $time;
  }

  function pageFinishTime($start) {
    $time = microtime();
    $time = explode(' ', $time);
    $time = $time[1] + $time[0];
    $finish = $time;
    $total_time = substr(round(($finish - $start), 1),0,2);
    $total_time = substr($total_time,0,1).'.'.substr($total_time,1,2);
    echo "Page generated in $total_time seconds.";
    echo 'Peak memory usage: ',round(memory_get_peak_usage()/1048576, 2), 'MB';
  }

  PageStartTime($start);
  pageFinishTime($start);

Output:- Page generated in 1.4 seconds.Peak memory usage: 0.37MB

Comments