Skip to content

Instantly share code, notes, and snippets.

@CNG
CNG / gist:2287416
Created April 2, 2012 21:39
PHP cache companion file
<?php
$max_age_seconds = 60*10;
$json_file = dirname(__FILE__).'/'.pathinfo(__FILE__,PATHINFO_FILENAME).'.json';
$data;
if( file_exists( $json_file ) && time() - filemtime( $json_file ) < $max_age_seconds ) {
$data = file_get_contents( $json_file );
} else {
// Create $data
$data = '';
@CNG
CNG / gist:1672310
Created January 24, 2012 20:16
Movable Type template timing
<$mt:Var name="time" value="1"$>
<mt:If name="time">
<$mt:Date format="%H" setvar="hours"$>
<$mt:Date format="%M" setvar="minutes"$>
<$mt:Date format="%S" setvar="seconds"$>
<$mt:Var name="hours" op="*" value="3600" setvar="hourseconds"$>
<$mt:Var name="minutes" op="*" value="60" setvar="minuteseconds"$>
<$mt:Var name="totalseconds" value="$hourseconds"$>
<$mt:Var name="totalseconds" op="+" value="$minuteseconds" setvar="totalseconds"$>
<$mt:Var name="totalseconds" op="+" value="$seconds" setvar="totalseconds"$>
@CNG
CNG / chooser.php
Last active April 20, 2017 08:30
Listing chooser
<?php
/*
This script takes URL parameters language_path, category, year and month and
assigns to variables $p, $c, $y and $m. It is assumed language_path and category
will consist of alphanumeric or underscore/hyphen characters, and the year and
month will be integers.
The user is redirected to the appropriate predictable archive listing URL if it
exists, otherwise user is redirected to a "no results" page.
TO DO: If user specifies a month but no year, currently no "invalid" message is
displayed, but rather we just redirect to the "no results" page.
@CNG
CNG / gist:1337577
Created November 3, 2011 19:44
Movable Type two paragraph excerpt with php
<?php
$body = <<<EOT
<$mt:EntryBody encode_php="here"$>
EOT;
preg_match_all('#<p>(.+?)</p>#is', $body, $matches);
switch(count($matches[0])) {
case 0: break;
case 1: echo $matches[0][0]; break;
case 2: echo $matches[0][0].$matches[0][1]; break;
default: echo $matches[0][0]."<p>".$matches[1][1].' <a href="<mt:EntryPermalink>" class="actionlink">Read more &raquo;</a>'; break;