PHP code example of darkfriend / php5-helpers

1. Go to this page and download the library: Download darkfriend/php5-helpers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

darkfriend / php5-helpers example snippets


$json = [
    'param1' => 'value1',
    'param2' => 'value2',
    'param3' => 'value3',
];
\darkfriend\helpers\Json::encode($json); // string

$jsonString = '{"param1":"value1","param2":"value2","param3":"value3"}';
\darkfriend\helpers\Json::decode($jsonString); // array

// request body string
$body = \darkfriend\helpers\Request::getBody(); 

// get request body json
$body = \darkfriend\helpers\Request::getBodyJson();
var_dump($body); // all keys from body json

$jsonResponse = [
    'param1' => 'value1',
    'param2' => 'value2',
    'param3' => 'value3',
];
// json response
$body = \darkfriend\helpers\Response::json($jsonResponse); 
die($body);
// or response json string
\darkfriend\helpers\Response::json($jsonResponse,[
    'show' => true,
    'die' => true,
]);

// xml response
$body = \darkfriend\helpers\Response::xml($jsonResponse);
die($body);
// or response xml string
\darkfriend\helpers\Response::xml($jsonResponse,[
    'show' => true,
    'die' => true,
]);

// add header
\darkfriend\helpers\Response::setHeader([
    'Content-Type' => 'application/json',
    'Custom-Header' => 'custom header value',
]);

$age = \darkfriend\helpers\DateTimeHelper::getAge('1992-05-16'); // 28 (by from 3 october 2020)
// or
$age = \darkfriend\helpers\DateTimeHelper::getAge('1992-05-16', '2020-10-03'); // 28 (by from 3 october 2020)

$seconds = \darkfriend\helpers\DateTimeHelper::getAmountEndDay();

$endTime = strtotime("tomorrow") - 1;
$seconds = \darkfriend\helpers\DateTimeHelper::getAmountSeconds($endTime);