Skip to content

Instantly share code, notes, and snippets.

@quinncomendant
Created November 2, 2021 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quinncomendant/086d60aaa26465b41e37b0a4e543a2eb to your computer and use it in GitHub Desktop.
Save quinncomendant/086d60aaa26465b41e37b0a4e543a2eb to your computer and use it in GitHub Desktop.
Convert Period of Time values (e.g., 1h15m30s) to seconds. Converts all values in stdin and sends to stdout.
#!/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