Some util functions in PHP.

class Util {

public function isJson($string){
if (!empty($string)){
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
} else {
return false;
}
}

public function iif($condition, $true, $false ) {
return ($condition ? $true : $false);
}

public function date_with_micro($format, $timestamp = null) {

if (is_null($timestamp) || $timestamp === false) {
$timestamp = microtime(true);
}
$timestamp_int = (int)floor($timestamp);
$microseconds = (int)round($timestamp - floor($timestamp) * 10000000.0, 0);
$format_with_micro = str_replace("u", $microseconds, $format);

return date($format_with_micro, $timestamp_int);
}

public function object2array($object) { return @json_decode(@json_encode($object),1); }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s