2012-09-01 59 views
2

我usign strftime("%e %B %Y", 1344557429)PHP的strftime返回false

它返回false但應根據http://php.net/manual/en/function.strftime.php

任何想法,問題出在哪裏可能是這種格式"10 August 2012"返回的日期?

+0

「作爲輸出取決於底層C庫,不支持某些轉換說明符。在Windows上,提供未知的轉換說明iers將產生5個E_WARNING消息並返回** FALSE **。「也來自您正在指導的文檔 – PeeHaa

回答

3

閱讀說明書第一:

找到巨大的紅色框:

僅適用於Windows:的%e修飾符是不是在Windows 執行本支持功能。要達到此值,可以使用%#d 修飾符代替。下面的例子說明了如何編寫一個跨平臺兼容的函數。

雖然製作新代碼是將error_reporting設置爲E_ALL的常用操作,因此您可以輕鬆找到錯誤。

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); 
?> 
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/