Bohdan Zhuravel

twitter   facebook   goodreads   letterboxd   

Linux geek, web developer, Pixies fan. Utopian in beliefs. Hopeless romantic. Sitcom addict. Adore American culture and miss the 60's. Interested in practicing my English, so some posts may be in English while others are in Russian, depending on my mood. Feel free to correct me :)

May 13, 2012 at 11:57pm

Benchmarking and testing your PHP script with microtime

$timer = new Timer;
$timer->start();
// some code...
$timer->stop();
// some more code...
$timer->stop(true);

/**
 * PHP script execution timer
 * @author Bohdan Zhuravel
 * @version .1
 */
class Timer {

  private $time;

  const format = 
          "<strong>Execution Time</strong>: %s seconds<br />";

  public function start() {
    $this->_set();
  }

  public function stop($die = false) {
    $this->_get() or $this->_set();
    $s = round($this->_time() - $this->_get(), 4);
    $this->_set();
    printf(self::format, $s);
    $die and die;
  }

  private function _get() {
    return $this->time;
  }

  private function _set() {
    $this->time = $this->_time();
  }

  private function _time() {
    $time = explode(' ', microtime());
    $time = $time[1] + $time[0];
    return $time;
  }

}

#php #microtime

December 25, 2011 at 10:30pm

<?php
$today = date( "Y-m-d" );
$christmas = date( "Y-m-d", strtotime( "december 25" ) );
echo ( $today == $christmas ) ? "Merry Christmas!" : "";
?>

#php

December 20, 2011 at 8:23pm

#ruby #php #perl #java #in a nutshell #programming