【问题标题】:strftime returns me empty stringstrftime 给我返回空字符串
【发布时间】:2021-08-10 18:31:24
【问题描述】:

我有一个非常简单的错误,我不明白为什么 stftime PHP 函数 会返回一个 空字符串,而语法和代码似乎都正确。 p>

我得到了以下代码:

$next_high_tide_date = strtotime($next_high_tide_date);
// FROM '2021-05-22 00:26:00' TO (timestamp) 1621635960
$next_high_tide_date_day = strtoupper(strftime('%A %d %B', $next_high_tide_date));
// THIS RETURNS ME THE RIGHT DAY DATE
$next_high_tide_date_hour = strtoupper(strftime('%k H %M', $next_high_tide_date));
// THIS RETURNS ME AN EMPTY STRING

第一次声明后我的$next_hight_tide_date 值等于1621643160(时间戳)。 我检查了strftime checker online,我的代码是正确的,但在我的 php 页面中,仍然返回一个空字符串。

有人有想法吗?

【问题讨论】:

  • 您正在测试的$next_high_tide_date 的值是多少(原始值)?
  • 我用我在帖子中精确的时间戳进行测试:1621643160
  • 抱歉,是的,我意识到我不准确并编辑了评论。
  • 当前这是一个字符串2021-05-22 00:26:00。它可能会因 API 值而改变,但始终是这种形式。
  • 所以让我直截了当地说:您希望strtoupper(strftime('%k H %M', 1621635960)); 返回0 H 26,但它却返回''。在文档中,他们给出了关于 C 库不支持所有转换说明符的警告。不知道您将如何进行调试,但这是您可以遵循的线索。基于docs: %k 是有效格式。您是否尝试过以不同的方式设置语言环境?在文档中,我看到有人在做:setlocale(LC_TIME, 'fr_FR.utf8','fra');。您是否尝试过其他语言环境?希望这对您有所帮助。

标签: php timestamp strftime


【解决方案1】:

也许您应该使用date() 函数来格式化您的时间戳。

$next_high_tide_date = "2021-05-22 00:26:00";
$next_high_tide_date = strtotime($next_high_tide_date);
$next_high_tide_date_day = strtoupper(date('l d F', $next_high_tide_date));
$next_high_tide_date_hour = strtoupper(date('H \H i', $next_high_tide_date));

试试这个

或使用%H 代替%k

$next_high_tide_date = "2021-05-22 00:26:00";
$next_high_tide_date = strtotime($next_high_tide_date);
$next_high_tide_date_day = strtoupper(strftime('%A %d %B',$next_high_tide_date));
$next_high_tide_date_hour = strtoupper(strftime('%H H %M',$next_high_tide_date));

【讨论】:

  • 是的 strtotime() 需要一个字符串。我给它一个字符串。然后我把日期给我的 strftime()。顺便说一句,日期格式不像 strftime 格式,所以这无论如何都不起作用。我选择 strftime 而不是 date 是有原因的
  • @Kiji_T 这很令人困惑,因为您对参数和结果使用相同的变量。
  • 是的。我可以为示例更改它。
  • date() 函数在格式之前不使用%
  • 为什么你认为date() 会比strftime() 更成功?它们本质上是相同的,只是使用了不同样式的格式字符串。
猜你喜欢
  • 2020-09-14
  • 1970-01-01
  • 2020-01-27
  • 2020-07-11
  • 2015-05-25
  • 2011-05-09
  • 1970-01-01
  • 2012-07-20
  • 2011-12-15
相关资源
最近更新 更多