【问题标题】:Using PHP strftime() using date() format string使用 PHP strftime() 使用 date() 格式字符串
【发布时间】:2014-03-26 15:35:59
【问题描述】:

我正在维护 PHP 代码,这似乎很糟糕地搞乱了日期/时间处理。特别是此应用程序在其他语言环境中无法正常运行。

其中一个问题是使用不可本地化的date() 函数而不是strftime() 函数。它在函数内部使用了好几层,这些函数实际上被使用了数千次,并且到处都有许多不同的格式字符串。

strftime() 替换date() 会花费太多时间。相反,我正在寻找让strftime() 处理与date() 相同的格式字符串的方法,但找不到任何东西。是否有任何已知的解决方案可以将strftime()date() 格式字符串一起使用?

【问题讨论】:

  • 你能举例说明你的日期输出需要是什么样子吗?
  • 如果是“深几层”,date 生成的日期可能很适合内部使用; date 毕竟是生成机器可读日期格式的好方法。如果日期需要以不同的方式呈现给用户,那无论如何都应该只发生在“外层”。长话短说:请提供更多示例值的详细信息。
  • 您是否考虑过使用sed 在所有PHP 文件中查找和替换date()
  • 它用于内部和显示使用,范围从转换为 Unix 时间戳到“2013 年 5 月 12 日 12:34”格式以在屏幕上使用。
  • 这种方法的真正问题是 strftime() 在 PHP 8.1 中已被弃用,并将在 PHP 9 中删除。使用 DateTime 不是一个好的选择,因为它也忽略了语言环境并且只生成英文输出。此时唯一真正的解决方案是转换为使用 IntDateFormatter::format(),它确实使用与 date() 相同的格式字符串。它需要安装和启用 PHP 的 Intl 扩展,但我怀疑这将很快普遍可用,除非更改 DateTime 以响应语言环境。

标签: php date localization date-format strftime


【解决方案1】:
/**
 * Convert strftime format to php date format
 * @param $strftimeformat
 * @return string|string[]
 * @throws Exception
 */
function strftime_format_to_date_format($strftimeformat){
    $unsupported = ['%U', '%V', '%C', '%g', '%G'];
    $foundunsupported = [];
    foreach($unsupported as $unsup){
        if (strpos($strftimeformat, $unsup) !== false){
            $foundunsupported[] = $unsup;
        }
    }
    if (!empty($foundunsupported)){
        throw new \Exception("Found these unsupported chars: ".implode(",", $foundunsupported).' in '.$strftimeformat);
    }
    // It is important to note that some do not translate accurately ie. lowercase L is supposed to convert to number with a preceding space if it is under 10, there is no accurate conversion so we just use 'g'
    $phpdateformat = str_replace(
        ['%a','%A','%d','%e','%u','%w','%W','%b','%h','%B','%m','%y','%Y', '%D',    '%F',   '%x', '%n', '%t', '%H', '%k', '%I', '%l', '%M', '%p', '%P', '%r' /* %I:%M:%S %p */, '%R' /* %H:%M */, '%S', '%T' /* %H:%M:%S */, '%X', '%z', '%Z',
            '%c', '%s',
            '%%'],
        ['D','l', 'd', 'j', 'N', 'w', 'W', 'M', 'M', 'F', 'm', 'y', 'Y', 'm/d/y', 'Y-m-d', 'm/d/y',"\n","\t", 'H', 'G', 'h', 'g', 'i', 'A', 'a', 'h:i:s A', 'H:i', 's', 'H:i:s', 'H:i:s', 'O', 'T',
            'D M j H:i:s Y' /*Tue Feb 5 00:45:10 2009*/, 'U',
            '%'],
        $strftimeformat
    );
    return $phpdateformat;
}

我写了这个函数,因为上面的答案都不够。处理大多数转换 - 易于扩展。

@见https://www.php.net/manual/en/function.date.phphttps://www.php.net/manual/en/function.strftime.php

【讨论】:

    【解决方案2】:

    我猜您需要将date() 函数当前使用的格式转换为strftime() 可以使用的格式。

    为此,我建议将str_replace() 与数组一起用作searchreplace 参数:

    $oldFormat = 'Y-m-d';
    
    $search  = array('Y', 'm', 'd');
    $replace = array('%Y', '%m', '%d');
    
    $newFormat = str_replace($search, $replace, $oldFormat);
    

    当然,您应该添加所有需要转换的搜索和替换词。

    【讨论】:

    • Nope:除了Ymd 之外,大多数其他字母在date()strftime() 之间具有不同的含义。这没有任何意义,但就是这样。
    • 只是通常不一致的 PHP,@T30
    【解决方案3】:

    从 date() 到 strftime() 的示例。感谢 Kick_the_BUCKET

    $search  = array('Y', 'y', 'M', 'm', 'D', 'd');
    $replace = array('%Y', '%y', '%B', '%b', '%A %d', '%A %d');
    
    $dateFormat = str_replace($search, $replace, $dateFormat); ?>
    echo $dateFormat
    

    【讨论】:

      猜你喜欢
      • 2013-04-21
      • 2011-01-20
      • 1970-01-01
      • 2023-03-18
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多