Slide 1

Slide 1 text

Time Travelling in PHP

Slide 2

Slide 2 text

Who? Adrian Hardy I've been doing this for 10+ years Currently enthused by: Zend Framework 2, Laravel Magma Digital Development Team Lead Purveyor of fine software solutions

Slide 3

Slide 3 text

What problems are we solving? Commence a process after 3 days of inactivity Re­evaluate an environment/record every month Prevent X from happening if Y happened > $threshold ago In general, process an environment / data at a future point

Slide 4

Slide 4 text

Dude, just use DateTime::diff() It's not about calculating elapsed time It's about testing your system Call me a sceptic, but I want to see it working

Slide 5

Slide 5 text

Surely Unit Testing could solve this?

Slide 6

Slide 6 text

Can we change existing code? d a t e ( ' Y - m - d ' ) ; t i m e ( ) ; n e w D a t e T i m e ( ) ; s t r t o t i m e ( " + $ r e l a t i v e _ m o d i f i e r " ) ; N O W ( ) ; / / M y S Q L ' s e n t o u r a g e In general, code which compares against "now" scuppers us

Slide 7

Slide 7 text

If I could start some projects again Banish anyone who uses MySQL's NOW() Extend DateTime, or better yet use "Carbon" Introduce indirection to determine "now" Allow "now" to be offset by a new DateTime https://github.com/briannesbitt/Carbon/pull/17

Slide 8

Slide 8 text

Let's do this Legacy code, no easy injection method without disruptive changes The most surefire way of testing this is by changing our PC's clock A little clumsy This leads to nasty problems on reboot Enter: A third party tool, which intercepts calls to Linux's c l o c k _ g e t t i m e

Slide 9

Slide 9 text

flux capacitor

Slide 10

Slide 10 text

simple code / / t e s t . p h p e c h o d a t e ( ' r ' ) ; s l e e p ( 1 0 ) ; e c h o d a t e ( ' r ' ) ; . / f l u x - c a p a c i t o r t e s t . p h p The application doesn't pause, but the clock advances

Slide 11

Slide 11 text

serving pages Couldn't get httpd to work with flux­capacitor :( But PHP 5.4's built in server does! . / f l u x - c a p a c i t o r - - i d l e n e s s = 1 1 0 0 0 0 0 0 0 - - p h p - S l o c a l h o s t : 8 0 8 0 We need to set the idleness to > 1 second to prevent flux capacitor truncating the one second socket timeout used by PHP's internal web server

Slide 12

Slide 12 text

questions