Created
November 2, 2021 19:02
Convert Period of Time values (e.g., 1h15m30s) to seconds. Converts all values in stdin and sends to stdout.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
$content = trim(file_get_contents('php://stdin', 'r')); | |
preg_match_all('/\b((?:[\d]+h)?(?:[\d]+m)?(?:[\d]+s))\b/i', $content, $matches, PREG_PATTERN_ORDER); | |
foreach ($matches[1] as $PTtime) { | |
$start = new DateTime(); | |
$end = clone $start; | |
$end->add(new DateInterval(sprintf('PT%s', strtoupper($PTtime)))); | |
$elapsed = $end->getTimestamp() - $start->getTimestamp(); | |
$content = preg_replace(sprintf('/\b%s\b/', $PTtime), $elapsed, $content); | |
} | |
echo $content; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment