【问题标题】:Wordpress: Convert seconds to minutes and secondsWordpress:将秒转换为分钟和秒
【发布时间】:2014-04-02 15:20:53
【问题描述】:

我使用了一个名为 duration 的自定义字段,其值以秒为单位。

要将其插入我使用的帖子中

<?php echo get_post_meta($post->ID, 'key', true); ?> <?php echo get_post_meta($post->ID, 'duration', true); ?>

我的问题是:如何将duration 值转换为分钟和秒?

谢谢!

【问题讨论】:

  • 用除法计算

标签: php wordpress time


【解决方案1】:

可能有一种更优雅的方法,但它是相当简单的数学运算。

$duration_in_seconds = get_post_meta($post->ID, 'duration', true);
$minutes = floor($duration_in_seconds / 60);
$seconds = $duration_in_seconds - (60 * $minutes);
echo $minutes . ":" . str_pad($seconds, 2, "0", STR_PAD_LEFT);

【讨论】:

  • 您可以使用模数来提取秒数:$seconds = $duration_in_seconds % 60;
  • @Diamondo25 同意,取模是获得余数的更简单方法。谢谢。
【解决方案2】:

这不是wordpress,你可以看看http://www.php.net/strtotime,在那里你会看到如何在php中转换时间,但首先你需要将它声明为变量:

所以

<?php 
$duration = get_post_meta($post->ID, 'duration', true);

// previous to PHP 5.1.0 you would compare with -1, instead of false
if (($timestamp = strtotime($duration)) === false) {
    echo "The string ($str) is not Date";
} else {
    echo date('l dS \o\f F Y h:i:s A', strtotime($duration));
}
?>

检查您的 php 版本以使用它们,并且您可以检查有关日期参考的更多信息。 http://www.w3schools.com/php/php_ref_date.asp

请确保您也有正确的 php 版本,一些小的修改可以在日期中找到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 2013-05-05
    相关资源
    最近更新 更多