PHP code example of bretterer / iso_duration_converter

1. Go to this page and download the library: Download bretterer/iso_duration_converter 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/ */

    

bretterer / iso_duration_converter example snippets


$converter = new \Bretterer\IsoDurationConverter\DurationParser();
$converter->parse('PT8S'); // Returns 8
$converter->parse('PT5M'); // Returns 300
$converter->parse('PT20H'); // Returns 72000
$converter->parse('PT6M4S'); // Returns 364

$converter->compose(8); // Returns PT8S
$converter->compose(300); // Returns PT5M
$converter->compose(7200); // Returns PT20H
$converter->compose(364); //Returns PT6M4S

$converter->parse('P5W'); // Returns 3024000
// To Returns Weeks, The second argument should be true
$converter->compose(3024000, true); // Returns P5W
$converter->compose(3024000); // Returns P35D

$converter->parse('Hello World'); // Throws 'Invalid Argument Exception' with Message 'Invalid Duration'
$converter->parse('P10Y10M10D'); // Throws 'Invalid Argument Exception' with Message 'Ambiguous Duration'