Wednesday 30 July 2014

Ultimo

Can't seem to find this but I've been using it a lot lately:
<?php
$ultimo = array(
    "description" => "Year, month, first and last days for last month",
    "year" => date("Y", strtotime("-1 months")),
    "month" => date("m", strtotime("-1 months")),
    "first" => date("d", mktime(0, 0, 0, date("m")-1, 1, date("Y"))),
    "last" => date("d", mktime(0, 0, 0, date("m"), 0, date("Y")))
);
?>

Thursday 24 July 2014

PHP in_array test (checking for an array in an array or array)

This pleased me no end:

<?php
    // Our original array
    $things = array(
        array(
            "One" => 1,
            "Two" => 2,
            "Three" => 3
        ),
        array(
            "Four" => 4,
            "Five" => 5,
            "Six" => 6
        )
    );
    // Isn't it lovely
    var_dump($things);
    // An array which isn't in our array
    $thing3 = array(
        "Seven" => 7,
        "Eight" => 8,
        "Nine" => 9
    );
    var_dump(in_array($thing3, $things)); // boolean false
    // An array which is in our array
    $thing1 = array(
        "One" => 1,
        "Two" => 2,
        "Three" => 3
    );
    var_dump(in_array($thing1, $things)); // boolean true
    // An array which is in our array but is in the wrong order
    $thing2 = array(
        "Six" => 6,
        "Five" => 5,
        "Four" => 4
    );
    var_dump(in_array($thing2, $things)); // boolean true
?>

Tuesday 22 July 2014

Can't 'ave too many bits of paper...

...except you can when you're used to living afloat where paper has a tendency to get wet and rot and so you want everything online!

Friday 4 July 2014

Score from Monday

Name: Katrina Myers
Date: 30 June 2014
Recurve Bow
Distance: 12yds
ET ET Doz
09 09 08 04 10 09 49 07 06 06 06 06 04 35 84
09 08 06 06 09 07 45 04 03 09 08 08 06 38 83
RT
167
Name: Dominic Myers
Date: 30 June 2014
Recurve Bow
Distance: 12yds
ET ET Doz
10 10 08 06 10 09 53 09 06 10 10 09 09 53 106
10 09 09 08 10 10 56 09 10 10 09 09 08 55 111
RT
217
Thank you Witchford Archers

Wednesday 2 July 2014

So very proud!

This came via email today and I couldn't be more pleased! +Ash has worked like a dog since last Summer and has finished secondary education! WOW!

I've got to hand it to him (and +Katrina) for all the hard work they've put in. Well done the two of you!

Tuesday 1 July 2014

Javascript: Iteration speed test ($.grep for teh win!)

So +Mehdi Rahman and I were trying to work out which was fastest... turns out $.grep wins and the for loop and the $.each perform differently on Chrome and FireFox. Quite a lot differently!


var start = new Date().getTime();
for (b = 0; b < 50000; ++b) {
 var a = ["1","2","1","2","1","2","1","2","1","2"];
 for(var i = 0; i < a.length; i++){
  if(a[i] === "1"){
   a.splice(i, 1);
  }
 }
}
var end = new Date().getTime();
var time = end - start;
alert('Execution time: ' + time);

var start = new Date().getTime();
for (b = 0; b < 50000; ++b) {
 var a = ["1","2","1","2","1","2","1","2","1","2"];
 $.each(a, function(k, v){
  if(v){
   if(v === "1"){
    a.splice(k, 1);
   }
  }else{
   return false
  }
 });
}
var end = new Date().getTime();
var time = end - start;
alert('Execution time: ' + time);

var start = new Date().getTime();
for (b = 0; b < 50000; ++b) {
 var a = ["1","2","1","2","1","2","1","2","1","2"];
 $.grep(a, function( n, i ) {
  return ( n !== "1" );
 });
}
var end = new Date().getTime();
var time = end - start;
alert('Execution time: ' + time);

Javascript: Convert text to HTML Number

I needed to display a JSON object on screen within a Tooltipster tooltip and I was pulling my hair out trying to figure out how to so it... Then I came across this lovely post. Shamelessly stealing the process I wrote my own as, for some reason, I prefer HTML Numbers rather than unicode:

function preJSON(json){
    var pre = "<pre>";
    var erp = "</pre>";
    var br = "<br />";
    var quote = '"';
    var space = " ";
    var jsonString = JSON.stringify(json, undefined, 2).replace(/"/g, toHTMLNumber(quote)).replace(/\n\r?/g, toHTMLNumber(br)).replace(/ /g, toHTMLNumber(space));
    return toHTMLNumber(pre) + jsonString + toHTMLNumber(erp);
    function toHTMLNumber(theString){
        var HTMLNumberString = '';
        for (var i=0; i < theString.length; i++) {
            var theHTMLNumber = theString.charCodeAt(i).toString(10).toUpperCase();
            theHTMLNumber = '&#' + theHTMLNumber + ';';
            HTMLNumberString += theHTMLNumber;
        }
        return HTMLNumberString;
    }
}

And then I call tooltipster with "contentAsHTML": true and it works a treat.