Skip to content

Instantly share code, notes, and snippets.

View pavlepredic's full-sized avatar

Pavle Predic pavlepredic

  • Beograd, Serbia
View GitHub Profile
@pavlepredic
pavlepredic / gist:6220041
Created August 13, 2013 10:53
TimeInterval class
<?php
/**
* Representation of a time interval, such as 1 year,
* or a more complex interval such as 1 month, 4 days and 6 hours.
* Provides methods for adding and substracting this interval
* to/from a given DateTime object or to/from a UNIX timestamp.
* Sample usage:
* $int = new TimeInterval(1, TimeInterval::YEAR); //1 year
* $int->addInterval(1, TimeInterval::MONTH); //allows creating more complex intervals (optional)
* $future = $int->addToDate(); //$future will be a DateTime object set to 1 year and 1 month from now
@pavlepredic
pavlepredic / PHP interactive shell
Created March 28, 2013 14:17
Interactive PHP shell. In Windows, running php in interactive mode is pretty quirky. This script attempts to solve this problem. It allows entering entire function definitions and classes. You may enter code on multiple lines. Nothing will be evaluated until you enter a semicolon. Save this to a file and run it from console. Hit CTRL+C to exit.
<?php
$fh = fopen('php://stdin', 'r');
$cmd = '';
$bcLvl = 0;
while (true)
{
$line = rtrim(fgets($fh));
$bcLvl += substr_count($line, '{') - substr_count($line, '}');
$cmd.= $line;
if ($bcLvl > 0 or substr($cmd, -1) !== ';')