Skip to content

Instantly share code, notes, and snippets.

@anthonybudd
Last active March 9, 2017 10:27
Show Gist options
  • Save anthonybudd/884eb003fa5596c1854e2539635fe407 to your computer and use it in GitHub Desktop.
Save anthonybudd/884eb003fa5596c1854e2539635fe407 to your computer and use it in GitHub Desktop.
<?php
Class City extends WP_Model{
public $postType = 'city';
public $attributes = [
'population',
];
public $filter = [
'population'
];
public function _filterPopulation($value){
$suffixes = ['', 'K', 'M', 'G', 'T'];
$suffixIndex = 0;
while(abs($value) >= 1000 && $suffixIndex < sizeof($suffixes)){
$suffixIndex++;
$value /= 1000;
}
return ( $value > 0
? floor($value * 1000) / 1000
: ceil($value * 1000) / 1000
) . $suffixes[$suffixIndex];
}
}
$london = City::insert([
'title' => 'London',
'population' => '8674000'
]);
echo $london->population; // 8.674M
$reykjavik = City::insert([
'title' => 'reykjavik',
'population' => '119200'
]);
echo $reykjavik->population; // 119.2K
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment