PHP strftime返回false

时间:2012-09-01 18:05:03

标签: php strftime

我正在使用strftime("%e %B %Y", 1344557429)

它会返回false,但它应根据http://php.net/manual/en/function.strftime.php

以此格式"10 August 2012"返回日期

可能存在问题的任何想法?

3 个答案:

答案 0 :(得分:4)

首先阅读手册:

找到一个巨大的红色框:

  

仅限Windows: Windows不支持%e修饰符   执行此功能。要实现此值,%#d   可以使用修饰符。以下示例说明了如何操作   编写跨平台兼容的功能。

虽然创建新代码是常见做法,但将error_reporting设置为E_ALL,这样您就可以轻松找到错误。

答案 1 :(得分:2)

对于单日期使用:

$format = '%B '.((strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') ? '%#d' : '%e').', %Y';
$date = strftime($format, $unix_timestamp);

PHP文档解决方案功能很好:

function fixed_strftime($format, $unix_timestamp) {
    if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
        $format = preg_replace('#(?<!%)((?:%%)*)%e#', '\1%#d', $format);
    }
    return strftime($format, $unix_timestamp);
}

来源:http://codeitdown.com/php-strftime-e-on-windows/

答案 2 :(得分:0)

啊,找到了解决方案,感谢@Peter Szymkowski。我是盲目的

<?php

// Jan 1: results in: '%e%1%' (%%, e, %%, %e, %%)
$format = '%%e%%%e%%';

// Check for Windows to find and replace the %e 
// modifier correctly
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
    $format = preg_replace('#(?<!%)((?:%%)*)%e#', '\1%#d', $format);
}

echo strftime($format);
?>
相关问题