Skip to content

Instantly share code, notes, and snippets.

@ve3
Last active October 23, 2025 15:29
Show Gist options
  • Select an option

  • Save ve3/a3b7924a85c3286b554f9e636b919883 to your computer and use it in GitHub Desktop.

Select an option

Save ve3/a3b7924a85c3286b554f9e636b919883 to your computer and use it in GitHub Desktop.

Revisions

  1. ve3 renamed this gist Mar 27, 2022. 1 changed file with 0 additions and 0 deletions.
  2. ve3 revised this gist Dec 17, 2021. 1 changed file with 59 additions and 21 deletions.
    80 changes: 59 additions & 21 deletions strftome-to-intldateformatter-converter.php
    Original file line number Diff line number Diff line change
    @@ -63,36 +63,30 @@
    $calendar = null;

    if (!empty($format)) {
    $output = $format;
    $pattern = $format;
    // replace 1 single quote that is not following visible character or single quote and not follow by single quote or word or number.
    // example: '
    // replace with 2 single quotes. example: ''
    $output = preg_replace('/(?<![\'\S])(\')(?![\'\w])/u', "'$1", $output);
    $pattern = preg_replace('/(?<![\'\S])(\')(?![\'\w])/u', "'$1", $pattern);
    // replace 1 single quote that is not following visible character or single quote and follow by word.
    // example: 'xx
    // replace with 2 single quotes. example: ''xx
    $output = preg_replace('/(?<![\'\S])(\')(\w+)/u', "'$1$2", $output);
    $pattern = preg_replace('/(?<![\'\S])(\')(\w+)/u', "'$1$2", $pattern);
    // replace 1 single quote that is following word (a-z 0-9) and not follow by single quote.
    // example: xx'
    // replace with 2 single quotes. example: xx''
    $output = preg_replace('/([\w]+)(\')(?!\')/u', "$1'$2", $output);
    $pattern = preg_replace('/([\w]+)(\')(?!\')/u', "$1'$2", $pattern);
    // replace a-z (include upper case) that is not following %. example xxx.
    // replace with wrap single quote. example: 'xxx'.
    $output = preg_replace('/(?<![%a-zA-Z])([a-zA-Z]+)/u', "'$1$2'", $output);
    $pattern = preg_replace('/(?<![%a-zA-Z])([a-zA-Z]+)/u', "'$1$2'", $pattern);

    // escape %%x with '%%x'.
    $output = preg_replace('/(%%[a-zA-Z]+)/u', "'$1'", $output);
    $pattern = preg_replace('/(%%[a-zA-Z]+)/u', "'$1'", $pattern);

    foreach ($replaces as $strftime => $intl) {
    $output = preg_replace('/(?<!%)(' . $strftime . ')/u', $intl, $output);
    $pattern = preg_replace('/(?<!%)(' . $strftime . ')/u', $intl, $pattern);
    }// endforeach;
    unset($intl, $strftime);

    // in case other format the left the replacement such as %f.
    // replace a-z (include upper case) that is follwing % but not following %% or '%.
    // example: %f NOT %%f, '%
    // replace with wrap single quote. example: %'f'.
    $output = preg_replace('/(?<![%\'])(%)([a-zA-Z]+)/u', "$1'$2'", $output);
    }

    if (class_exists('IntlDateFormatter')) {
    @@ -108,6 +102,40 @@
    unset($tz);
    }

    // set locales for select box.
    $locales = [
    'en_US.utf-8',
    'en_US.utf8',
    'en_us.utf-8',
    'en_us.utf8',
    'en-US.utf-8',
    'en-US.utf8',
    'en-us.utf-8',
    'en-us.utf8',
    'en.utf-8',
    'en.utf8',
    'en_US',
    'en_us',
    'en-US',
    'en-us',
    'en',

    'th_TH.utf-8',
    'th_TH.utf8',
    'th_th.utf-8',
    'th_th.utf8',
    'th-TH.utf-8',
    'th-TH.utf8',
    'th-th.utf-8',
    'th-th.utf8',
    'th.utf-8',
    'th.utf8',
    'th_TH',
    'th_th',
    'th-TH',
    'th-th',
    'th',
    ];
    ?>
    <!DOCTYPE html>
    <html>
    @@ -150,8 +178,18 @@
    <div>
    Locale:
    <select name="locale">
    <option value="en_US"<?php if ($locale === 'en_US') {echo ' selected';} ?>>en_US</option>
    <option value="th.utf-8"<?php if ($locale === 'th.utf-8') {echo ' selected';} ?>>th.utf-8</option>
    <?php
    if (isset($locales) && is_array($locales)) {
    foreach ($locales as $eachLocale) {
    echo '<option value="' . $eachLocale . '"';
    if ($locale === $eachLocale) {
    echo ' selected';
    }
    echo '>' . $eachLocale . '</option>' . PHP_EOL;
    }// endforeach;
    unset($eachLocale);
    }
    ?>
    </select>
    </div>
    <div>
    @@ -169,21 +207,21 @@
    </form>

    <?php
    if (isset($output)) {
    if (isset($pattern)) {
    ?>
    <h3>Converted result</h3>
    <pre><?php
    echo htmlspecialchars($output, ENT_QUOTES);
    echo htmlspecialchars($pattern, ENT_QUOTES);
    ?></pre>
    <h3>Execute</h3>
    <?php if (function_exists('strftime')) { ?>
    <p><code>strftime()</code>: <strong><?php echo htmlspecialchars(@strftime($format), ENT_QUOTES); ?></strong></p>
    <p><code>strftime('<?php echo $format; ?>')</code>: <strong><?php echo htmlspecialchars(@strftime($format), ENT_QUOTES); ?></strong></p>
    <?php } ?>
    <?php
    if (isset($IntlDateFormatter)) {
    $IntlDateFormatter->setPattern($output);
    $IntlDateFormatter->setPattern($pattern);
    ?>
    <p><code>IntlDateFormatter()</code>: <strong><?php echo htmlspecialchars($IntlDateFormatter->format(time()), ENT_QUOTES); ?></strong></p>
    <p><code>IntlDateFormatter::setPattern('<?php echo $pattern; ?>')</code>: <strong><?php echo htmlspecialchars($IntlDateFormatter->format(time()), ENT_QUOTES); ?></strong></p>
    <?php
    }
    ?>
    @@ -231,7 +269,7 @@
    <td><code><?php echo htmlspecialchars($strftime, ENT_QUOTES); ?></code></td>
    <td><?php
    if (isset($strftimeResult)) {
    echo htmlspecialchars($strftimeResult);
    echo htmlspecialchars($strftimeResult, ENT_QUOTES);
    }
    ?></td>
    <td><code><?php echo htmlspecialchars($intl, ENT_QUOTES); ?></code></td>
  3. ve3 revised this gist Dec 17, 2021. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions strftome-to-intldateformatter-converter.php
    Original file line number Diff line number Diff line change
    @@ -89,10 +89,10 @@
    unset($intl, $strftime);

    // in case other format the left the replacement such as %f.
    // replace a-z (include upper case) that is follwing % but not following %%.
    // example: %f NOT %%f
    // replace a-z (include upper case) that is follwing % but not following %% or '%.
    // example: %f NOT %%f, '%
    // replace with wrap single quote. example: %'f'.
    $output = preg_replace('/(?<![%])(%)([a-zA-Z]+)/u', "$1'$2'", $output);
    $output = preg_replace('/(?<![%\'])(%)([a-zA-Z]+)/u', "$1'$2'", $output);
    }

    if (class_exists('IntlDateFormatter')) {
  4. ve3 revised this gist Dec 17, 2021. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions strftome-to-intldateformatter-converter.php
    Original file line number Diff line number Diff line change
    @@ -87,6 +87,12 @@
    $output = preg_replace('/(?<!%)(' . $strftime . ')/u', $intl, $output);
    }// endforeach;
    unset($intl, $strftime);

    // in case other format the left the replacement such as %f.
    // replace a-z (include upper case) that is follwing % but not following %%.
    // example: %f NOT %%f
    // replace with wrap single quote. example: %'f'.
    $output = preg_replace('/(?<![%])(%)([a-zA-Z]+)/u', "$1'$2'", $output);
    }

    if (class_exists('IntlDateFormatter')) {
  5. ve3 revised this gist Dec 17, 2021. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions strftome-to-intldateformatter-converter.php
    Original file line number Diff line number Diff line change
    @@ -80,6 +80,9 @@
    // replace with wrap single quote. example: 'xxx'.
    $output = preg_replace('/(?<![%a-zA-Z])([a-zA-Z]+)/u', "'$1$2'", $output);

    // escape %%x with '%%x'.
    $output = preg_replace('/(%%[a-zA-Z]+)/u', "'$1'", $output);

    foreach ($replaces as $strftime => $intl) {
    $output = preg_replace('/(?<!%)(' . $strftime . ')/u', $intl, $output);
    }// endforeach;
  6. ve3 revised this gist Dec 17, 2021. 1 changed file with 21 additions and 8 deletions.
    29 changes: 21 additions & 8 deletions strftome-to-intldateformatter-converter.php
    Original file line number Diff line number Diff line change
    @@ -63,12 +63,25 @@
    $calendar = null;

    if (!empty($format)) {
    $matchPattern = '(?<!%)(%a)';
    $outputFormat = $format;
    $outputFormat = preg_replace('/(%%[a-zA-Z])/u', "'$1'", $outputFormat);// escape %%x with '%%x'.
    $outputFormat = preg_replace('/(?<!%)([a-zA-Z]+)/u', "'$1$2'", $outputFormat);// escape xx that has no % in the front.
    $output = $format;
    // replace 1 single quote that is not following visible character or single quote and not follow by single quote or word or number.
    // example: '
    // replace with 2 single quotes. example: ''
    $output = preg_replace('/(?<![\'\S])(\')(?![\'\w])/u', "'$1", $output);
    // replace 1 single quote that is not following visible character or single quote and follow by word.
    // example: 'xx
    // replace with 2 single quotes. example: ''xx
    $output = preg_replace('/(?<![\'\S])(\')(\w+)/u', "'$1$2", $output);
    // replace 1 single quote that is following word (a-z 0-9) and not follow by single quote.
    // example: xx'
    // replace with 2 single quotes. example: xx''
    $output = preg_replace('/([\w]+)(\')(?!\')/u', "$1'$2", $output);
    // replace a-z (include upper case) that is not following %. example xxx.
    // replace with wrap single quote. example: 'xxx'.
    $output = preg_replace('/(?<![%a-zA-Z])([a-zA-Z]+)/u', "'$1$2'", $output);

    foreach ($replaces as $strftime => $intl) {
    $outputFormat = preg_replace('/(?<!%)(' . $strftime . ')/u', $intl, $outputFormat);
    $output = preg_replace('/(?<!%)(' . $strftime . ')/u', $intl, $output);
    }// endforeach;
    unset($intl, $strftime);
    }
    @@ -147,19 +160,19 @@
    </form>

    <?php
    if (isset($outputFormat)) {
    if (isset($output)) {
    ?>
    <h3>Converted result</h3>
    <pre><?php
    echo htmlspecialchars($outputFormat, ENT_QUOTES);
    echo htmlspecialchars($output, ENT_QUOTES);
    ?></pre>
    <h3>Execute</h3>
    <?php if (function_exists('strftime')) { ?>
    <p><code>strftime()</code>: <strong><?php echo htmlspecialchars(@strftime($format), ENT_QUOTES); ?></strong></p>
    <?php } ?>
    <?php
    if (isset($IntlDateFormatter)) {
    $IntlDateFormatter->setPattern($outputFormat);
    $IntlDateFormatter->setPattern($output);
    ?>
    <p><code>IntlDateFormatter()</code>: <strong><?php echo htmlspecialchars($IntlDateFormatter->format(time()), ENT_QUOTES); ?></strong></p>
    <?php
  7. ve3 revised this gist Dec 9, 2021. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions strftome-to-intldateformatter-converter.php
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    /**
    * PHP `strftime()` format to `IntlDateFormatter()` pattern converter.
    *
    * PHP `strftime()` was deprecated since v 8.1. They recommended to use `IntlDateFormatter()` instead.
    * PHP `strftime()` is deprecated since v 8.1. They recommended to use `IntlDateFormatter()` instead.
    * However `IntlDateFormatter()` pattern does not fully supported all format that `strftime()` does.
    *
    * Run this file to get the result of most close pattern to `strftime()` based on Thai locale.
    @@ -65,6 +65,8 @@
    if (!empty($format)) {
    $matchPattern = '(?<!%)(%a)';
    $outputFormat = $format;
    $outputFormat = preg_replace('/(%%[a-zA-Z])/u', "'$1'", $outputFormat);// escape %%x with '%%x'.
    $outputFormat = preg_replace('/(?<!%)([a-zA-Z]+)/u', "'$1$2'", $outputFormat);// escape xx that has no % in the front.
    foreach ($replaces as $strftime => $intl) {
    $outputFormat = preg_replace('/(?<!%)(' . $strftime . ')/u', $intl, $outputFormat);
    }// endforeach;
    @@ -138,7 +140,8 @@
    </select>
    </div>
    <div>
    <code>strftime()</code> format: <input type="text" name="format" value="<?php echo htmlspecialchars($format, ENT_QUOTES); ?>">
    <code>strftime()</code> format:
    <input type="text" name="format" value="<?php echo htmlspecialchars($format, ENT_QUOTES); ?>" style="width: 300px;">
    </div>
    <button type="submit">Submit</button>
    </form>
  8. ve3 created this gist Dec 8, 2021.
    235 changes: 235 additions & 0 deletions strftome-to-intldateformatter-converter.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,235 @@
    <?php
    /**
    * PHP `strftime()` format to `IntlDateFormatter()` pattern converter.
    *
    * PHP `strftime()` was deprecated since v 8.1. They recommended to use `IntlDateFormatter()` instead.
    * However `IntlDateFormatter()` pattern does not fully supported all format that `strftime()` does.
    *
    * Run this file to get the result of most close pattern to `strftime()` based on Thai locale.
    *
    * @license MIT
    */


    $locale = ($_POST['locale'] ?? 'en_US');
    setlocale(LC_ALL, $locale);

    // replace from `strftime` format to `IntlDateFormatter` pattern.
    // based on Thai locale.
    $replaces = [
    '%a' => 'E',
    '%A' => 'EEEE',
    '%d' => 'dd',
    '%e' => 'd',
    '%j' => 'D',
    '%u' => 'e',// not 100% correct
    '%w' => 'c',// not 100% correct
    '%U' => 'w',
    '%V' => 'ww',// not 100% correct
    '%W' => 'w',// not 100% correct
    '%b' => 'MMM',
    '%B' => 'MMMM',
    '%h' => 'MMM',// alias of %b
    '%m' => 'MM',
    '%C' => 'yy',// no replace for this
    '%g' => 'yy',// no replace for this
    '%G' => 'Y',// not 100% correct
    '%y' => 'yy',
    '%Y' => 'yyyy',
    '%H' => 'HH',
    '%k' => 'H',
    '%I' => 'hh',
    '%l' => 'h',
    '%M' => 'mm',
    '%p' => 'a',
    '%P' => 'a',// no replace for this
    '%r' => 'hh:mm:ss a',
    '%R' => 'HH:mm',
    '%S' => 'ss',
    '%T' => 'HH:mm:ss',
    '%X' => 'HH:mm:ss',// no replace for this
    '%z' => 'ZZ',
    '%Z' => 'v',// no replace for this
    '%c' => 'd/M/YYYY HH:mm:ss',// Buddhist era not converted.
    '%D' => 'MM/dd/yy',
    '%F' => 'yyyy-MM-dd',
    '%s' => '',// no replace for this
    '%x' => 'd/MM/yyyy',// Buddhist era not converted.
    '%n' => "\n",
    '%t' => "\t",
    '%%' => '%',
    ];
    $format = ($_POST['format'] ?? '');
    $calendar = null;

    if (!empty($format)) {
    $matchPattern = '(?<!%)(%a)';
    $outputFormat = $format;
    foreach ($replaces as $strftime => $intl) {
    $outputFormat = preg_replace('/(?<!%)(' . $strftime . ')/u', $intl, $outputFormat);
    }// endforeach;
    unset($intl, $strftime);
    }

    if (class_exists('IntlDateFormatter')) {
    $tz = null;
    $calendar = ($_POST['calendar'] ?? null);
    if ($calendar === 'default') {
    $calendar = null;
    } elseif ($calendar === 'traditional') {
    $calendar = \IntlDateFormatter::TRADITIONAL;
    }
    $IntlDateFormatter = new \IntlDateFormatter($locale, \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, $tz, $calendar);
    $defaultPattern = $IntlDateFormatter->getPattern();
    unset($tz);
    }

    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>strftime to IntlDateFormatter format converter</title>
    <style type="text/css">
    code {
    background-color: #eee;
    padding: 2px 5px;
    }
    form {
    margin: 0 0 20px;
    }
    form > div {
    margin: 0 0 10px;
    }
    pre {
    background-color: #eee;
    padding: 10px;
    }
    table {
    border: 1px solid #ccc;
    border-collapse: collapse;
    width: 100%;
    }
    table th,
    table td {
    border: 1px solid #ccc;
    text-align: left;
    padding: 5px;
    }
    table tr.different-result {
    background-color: rgba(255, 218, 148, 0.4);
    }
    </style>
    </head>
    <bodY>
    <form method="post">
    <div>
    Locale:
    <select name="locale">
    <option value="en_US"<?php if ($locale === 'en_US') {echo ' selected';} ?>>en_US</option>
    <option value="th.utf-8"<?php if ($locale === 'th.utf-8') {echo ' selected';} ?>>th.utf-8</option>
    </select>
    </div>
    <div>
    Calendar:
    <select name="calendar">
    <option value="default"<?php if ($calendar === 'default') {echo ' selected';} ?>>Default</option>
    <option value="traditional"<?php if ($calendar === 'traditional') {echo ' selected';} ?>>Traditional</option>
    </select>
    </div>
    <div>
    <code>strftime()</code> format: <input type="text" name="format" value="<?php echo htmlspecialchars($format, ENT_QUOTES); ?>">
    </div>
    <button type="submit">Submit</button>
    </form>

    <?php
    if (isset($outputFormat)) {
    ?>
    <h3>Converted result</h3>
    <pre><?php
    echo htmlspecialchars($outputFormat, ENT_QUOTES);
    ?></pre>
    <h3>Execute</h3>
    <?php if (function_exists('strftime')) { ?>
    <p><code>strftime()</code>: <strong><?php echo htmlspecialchars(@strftime($format), ENT_QUOTES); ?></strong></p>
    <?php } ?>
    <?php
    if (isset($IntlDateFormatter)) {
    $IntlDateFormatter->setPattern($outputFormat);
    ?>
    <p><code>IntlDateFormatter()</code>: <strong><?php echo htmlspecialchars($IntlDateFormatter->format(time()), ENT_QUOTES); ?></strong></p>
    <?php
    }
    ?>
    <?php
    }// endif;
    ?>

    <p>Current locale: <code><?php echo $locale; ?></code></p>
    <?php
    if (isset($IntlDateFormatter)) {
    $IntlDateFormatter->setPattern($defaultPattern);
    ?>
    <p>
    Default <code>IntlDateFormatter()</code> pattern (<code><?php echo $defaultPattern; ?></code>): <strong><?php echo $IntlDateFormatter->format(time()); ?></strong>
    </p>
    <?php
    }
    ?>
    <table>
    <thead>
    <tr>
    <th><code>strftime()</code> format</th>
    <th><code>strftime()</code> result</th>
    <th><code>IntlDateFormatter()</code> pattern</th>
    <th><code>IntlDateFormatter()</code> result</th>
    </tr>
    </thead>
    <tbody>
    <?php
    foreach ($replaces as $strftime => $intl) {
    if (function_exists('strftime')) {
    $strftimeResult = @strftime($strftime);
    }
    if (isset($IntlDateFormatter)) {
    $IntlDateFormatter->setPattern($intl);
    $intlDResult = $IntlDateFormatter->format(time());
    }
    if (isset($strftimeResult) && isset($intlDResult)) {
    if ($strftimeResult != $intlDResult) {
    $rowDif = true;
    }
    }
    ?>
    <tr<?php if (isset($rowDif)) { echo ' class="different-result"'; } ?>>
    <td><code><?php echo htmlspecialchars($strftime, ENT_QUOTES); ?></code></td>
    <td><?php
    if (isset($strftimeResult)) {
    echo htmlspecialchars($strftimeResult);
    }
    ?></td>
    <td><code><?php echo htmlspecialchars($intl, ENT_QUOTES); ?></code></td>
    <td><?php
    if (isset($intlDResult)) {
    echo htmlspecialchars($intlDResult, ENT_QUOTES);
    }
    ?></td>
    </tr>
    <?php
    unset($intlDResult, $rowDif, $strftimeResult);
    }// endforeach;
    unset($intl, $strftime);
    ?>
    </tbody>
    </table>

    <p>Reference:
    <a href="https://www.php.net/manual/en/function.strftime.php" target="strftome">strftime format.</a>
    <a href="https://unicode-org.github.io/icu/userguide/format_parse/datetime/" target="ICU">ICU format</a>
    </p>
    </bodY>
    </html>
    <?php

    unset($IntlDateFormatter, $replaces);