Saturday, September 14, 2013

Dynamic Relative Date Range

About Dynamic Relative Date-time Range
Dynamic Relative Date-time Range (also known as a Rolling Date Range) is a string formatted representation of a period of time relative to the present time in string form (like in an HTML form). Generally DRDR would be used in reporting environments as it provides a dynamic way to allow a predetermined selection.

Using this form allows coders and non-coders to easily read and write relative ranges. This can also be easily stored in a database for automated reporting.

This syntax requires a language with support similar to PHP's relative date syntax.

General Rules
Caret (^) represents the range separator always separating the starting relative date from the ending relative date. When you begin with a caret or end with a caret, the omitted value is assumed as "now".

last-year^ == last-year^now

The term "ago" can be provided as per PHP relative date syntax documentation to indicate a previous relative date or time.

12-months-ago^now (or) 12-months-ago^

DRDR Example Formats
jan-1^now (or) jan-1^
now^next-year-jan-1 (or) ^next-year-jan-1

HTML Example
<select name="date-range">
<option name="90-days-ago^now">Last 90 Days</option>
<option name="last-month^this-month">Last Month</option>
<option name="this-month^now">Month-to-Date</option>
<option name="30-days-ago^now">Last 30 Days</option>
<option name="last-week^this-week">Last Week</option>
<option name="this-week^now">Week-to-Date</option>
<option name="7-days-ago^now">Last 7 Days</option>
<option name="today^now">Today</option>

PHP Implementation
list($start_range, $end_range) = explode('^', $_REQUEST['date-range']);
$start_datetime = strtotime(str_replace('-', ' ', $start_range ?: 'now'));
$end_datetime = strtotime(str_replace('-', ' ', $end_range ?: 'now'));

Currently, I have plans to extends this format to include quarters, but have not yet done so. 

Thursday, September 5, 2013

Migrating From Laravel 3 to Laravel 4

I apologize that this page is not well organized. It mainly exists to serve as notes for myself. If you can make sense of this, great!

Changes I had to make to my code when updating from Laravel 3 to Laravel 4:

Auth::login(int) => Auth::attempt(array()) or Auth::login(User)
Request::current() => Request::path()
URI::segment(int) => Request::segment(int)
URL::to_route("X") => URL::route("X")
Request::route()->is("X") => Request::is("X")
Form::token() => included by default when calling Form::open() but still accessible
Form (method = POST) => default method when calling Form::open()
Form::open_for_files(...) => Form::open(array('files' => true, ...))
path(...) => storage_path() || public_path || app_path() || base_path()
with_errors($x) => withErrors($x)
with_input($x) => withInput($x)
Response::error() => App::abort()
Request::ip() => Request::getClientIp()
DB::query => DB::select, DB::insert, DB::update, DB::delete, DB::statement

@layout(...) => @extends(...)
@render(...) => @include(...)
@forelse => GONE, use @if(count(...))  and @foreach()  [ this was nice while it lasted :( ]

belongs_to => belongsTo
has_many => hasMany
belongs_to_many => belongsToMany
has_one => hasOne
order_by => orderBy
group_by => groupBy
has_many_and_belongs_to => hasManyAndBelongsTo

set_fieldx($val) {$this->field = $val;} => setFieldxAttribute($val) { $this->attributes['field'] = $val; }
get_fieldx(){return $this->field; } => getFieldxAttribute($val) { return $val; }
where_val() => whereVal()
or_where() => orWhere
where_in => whereIn
where_between => whereBetween (Note: 2nd argument should be array of min, max)
or_where_between => orWhereBetween (Note: 2nd argument should be array of min, max)
or_where_in => orWhereIn
where_not_in => whereNotIn
left_join => leftJoin
::hydrate() => newFromBuilder (read more)

You can add app/libraries to the composer autoload json or
create package(s) using workbench and place your code in there.

Error Running PEAR On Windows

I went to verify phpunit installation on my windows machine and found that when running pear.bat, I was met with the following:

$ pear.bat
./pear.bat: line 1: @ECHO: command not found
./pear.bat: line 3: REM: command not found
./pear.bat: line 4: REM: command not found
./pear.bat: line 5: REM: command not found
./pear.bat: line 6: syntax error near unexpected token `('
./pear.bat: line 6: `REM Copyright (c) 1997-2010 The Authors'

Reading the PEAR Documentation, I found that the program had to be 'run as Administrator'. Doing this allowed me to upgrade pear and then install phpunit.