strftime不显示PM或AM

时间:2011-07-28 00:41:28

标签: php strftime

这是我用来获取日期的代码(西班牙语):

$fecha = strftime("%d de %B, %Y, a las %l:%M %p");

我得到了:

27 de julio, 2011, a las 7:10

所以问题是,我的PM(或AM)在哪里?

我的应用程序在Linux上运行。

11 个答案:

答案 0 :(得分:6)

嗯,php manual

  

并非C库都支持所有转换说明符,在这种情况下,PHP的strftime()不支持它们。此外,并非所有平台都支持负时间戳,因此您的日期

这可能是你的问题。在这种情况下,你可以做这样的事情来获得AM / PM:

$time = time();
$fecha = strftime("%d de %B, %Y, a las %l:%M ", $time).(($time%86400) < 43200 ? 'AM' : 'PM');

修改

这个答案假设你想要一个UTC时间字符串(如果你想要另一个时区,你还应该在字符串中包含时区,如果你要存储它或发送到任何地方),并且你的php.ini有{{1设置为date.timezone。如果您不使用UTC,则应计算偏移量。 See @Soylent17's answer

答案 1 :(得分:3)

问题是陈旧的,但万一其他人需要它,它也发生在我身上。

似乎西班牙语语言环境没有为%p实现,因此请确保您的行位于将语言环境设置为西班牙语的行之前。可能你需要像这样分数你的代码。

 $hora = strftime(" a las %l:%M %p");
 $loc_sp = setlocale (LC_TIME,'es_ES');
 $fecha = strftime("%d de %B, %Y");
 echo $fecha.$hora;

答案 2 :(得分:3)

我没有足够的代表为PaulPro的答案添加评论,所以我只需要扩展它。直接使用时间戳时,您需要调整时区偏移量。所以在PaulPro放置$time%86400的地方,你需要像($time+date('Z'))%86400

这样的东西
$time = time();
$fecha = strftime("%d de %B, %Y, a las %l:%M ", $time).(($time+date('Z'))%86400 < 43200 ? 'AM' : 'PM');

答案 3 :(得分:2)

你在OSX上吗?如果是%p%P isn't supported

答案 4 :(得分:1)

您可以尝试使用setlocale()上的其他值,因为es_MX对我不起作用,es_CO完成工作,它支持%p和{{1} ,它取决于服务器配置:v

可能的值列表

答案 5 :(得分:0)

感谢@iyepes,我决定完全绕过Locale,如果你有多种语言的网站,%P或%p在我认为的英语以外的地方都没有价值,至少那是西班牙语案例

从语言环境中分离%P或%p,如果你有一个while循环,将locale设置为en_US,在en_US strftime中提取时间,包括%P,然后设置回用户定义的语言环境。

$day1 =  strtotime($row["day1"]);
$day2 = strtotime($row["day2"]);
$day3 = strtotime($row["day3"]);

  setlocale(LC_ALL, "en_US");

$time1 = strftime("%l:%M %P", $day1);
$time2 = strftime("%l:%M %P", $day2);
$time3 = strftime("%l:%M %P", $day3);

  setlocale(LC_ALL, $defined_locale);

$day1 - strftime("%d de %B, %Y, " $day1).$time1;
$day2 - strftime("%d de %B, %Y, " $day2).$time2;
etc...

答案 6 :(得分:0)

我的解决方案

function mystrftime($format, $time=null){
    if(!$time) $time = time();
    preg_match_all('/%[a-zA-Z]/', $format, $placeholders);
    foreach ($placeholders[0] as $placeholder){
        if(strftime($placeholder, $time)==''){
            $format = str_replace($placeholder, '%'.$placeholder, $format);
        }
    }   
    $format = strftime($format, $time);
    preg_match_all('/%[a-zA-Z]/', $format, $placeholders);
    $locale = setlocale('LC_TIME', '0');
    setlocale('LC_TIME', 'en_US.UTF8');
    foreach ($placeholders[0] as $placeholder){
        $format = str_replace($placeholder, strftime($placeholder, $time), $format);
    }   
    setlocale('LC_TIME', $locale);
    return $format;
}

答案 7 :(得分:0)

简单而丑陋

$date="2016-11-15 12:34";    
$fecha=strftime("%A, %d de %B de %Y %l:%M", strtotime("$date")).date(' a', strtotime("$date"));

结果:martes,15 de noviembre de 2016 12:34 pm

答案 8 :(得分:0)

确保区域设置拼写正确。对我来说,en_US.utf8正常工作,en_US根本不工作。 尝试通过调用

来检查unix计算机上的已安装语言环境
locale -a 

在命令提示符下。

答案 9 :(得分:0)

当我想出解决方法时,我正在这里寻找问题的答案。

这是我的默认语言设置。

// Set Default Timezone
date_default_timezone_set('America/Bogota');
// Set Date Language -> Unix
setlocale(LC_TIME, 'es_ES.UTF-8');
// Set Date Language -> Windows
setlocale(LC_TIME, 'spanish');

然后,我试图用AM和PM显示当前日期,但是它不起作用。

echo strftime("%d de %b de %Y, %H:%M %p", strtotime($sqlDate);

由于%p参数,该字段显示为空。当我阅读AgelessEssence的评论时,我开始在以下选项之间进行切换:

  • es_MX〜墨西哥
  • es_ES〜西班牙
  • es_RA〜阿根廷
  • es_CO〜哥伦比亚

当我以为我将这个字段留空并保存时,他们都没有工作。

 // Set Date Language -> Windows
    setlocale(LC_TIME, '');

然后,它就像一种魅力一样工作。

答案 10 :(得分:0)

        $formato = "%A, %e de %B de %Y, %I:%M:%S %p";
        // Comprobación para Windows para hallar y remplazar el
        // modificador %e adecuadamente
        if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
            $formato = preg_replace('#(?<!%)((?:%%)*)%e#', '\1%#d', $formato);
        }
        $formato = preg_replace('#(?<!%)((?:%%)*)%p#', '\1#p', $formato);
        $x = @strftime($formato);
        $y = date('A');
        $x = str_replace('#p', $y, $x);