将秒转换为分钟/秒,但语法正确

时间:2017-06-25 18:14:20

标签: php

例如,我有61秒,我想转换它,所以最终结果将是“1分1秒”。如果我有62秒,那么它将是“1分2秒 s ”并持续14秒只是“14秒”,你得到它。

是否有预制的PHP函数可以像这样转换秒,所以我不必自己制作一个?我不是在找gmdate()之类的东西,它会变成“01:02”,我希望它能成为一个句子中的字符串。

3 个答案:

答案 0 :(得分:3)

我使用DateInterval将秒数转换为年,月,日,小时,分钟和秒。

<?php
function convertSecondsToEnglish($num_seconds)
{
  if (!is_numeric($num_seconds) || $num_seconds < 0) {
    throw new Exception('Number of seconds must be greater than zero.');
  }
  $now = new DateTime();
  $future = new DateTime();
  $future->modify(sprintf('+%d second', $num_seconds));

  $diff = $future->diff($now);

  $components = [];
  // years
  if ($diff->y != 0) {
    $years = $diff->y;
    $components[] = sprintf('%d year%s', $years, ($years != 1) ? 's' : '');
  }
  // months
  if ($diff->m != 0) {
    $months = $diff->m;
    $components[] = sprintf('%d month%s', $months, ($months != 1) ? 's' : '');
  }
  // days
  if ($diff->d != 0) {
    $days = $diff->d;
    $components[] = sprintf('%d day%s', $days, ($days != 1) ? 's' : '');
  }
  // hours
  if ($diff->h != 0) {
    $hours = $diff->h;
    $components[] = sprintf('%d hour%s', $hours, ($hours != 1) ? 's' : '');
  }
  // minutes
  if ($diff->i != 0) {
    $mins = $diff->i;
    $components[] = sprintf('%d minute%s', $mins, ($mins != 1) ? 's' : '');
  }
  // seconds
  if ($diff->s != 0) {
    $seconds = $diff->s;
    $components[] = sprintf('%d second%s', $seconds, ($seconds != 1) ? 's' : '');
  }

  return implode(', ', $components);

}

示例:

echo convertSecondsToEnglish(3600) . PHP_EOL;
echo convertSecondsToEnglish(32) . PHP_EOL;
echo convertSecondsToEnglish(60) . PHP_EOL;
echo convertSecondsToEnglish(1234567) . PHP_EOL;
echo convertSecondsToEnglish(12345678) . PHP_EOL;
echo convertSecondsToEnglish(87654321) . PHP_EOL;

给出:

1 hour
32 seconds
1 minute
14 days, 6 hours, 56 minutes, 7 seconds
4 months, 19 days, 21 hours, 21 minutes, 18 seconds
2 years, 9 months, 9 days, 12 hours, 25 minutes, 21 seconds

演示:https://eval.in/822061

编辑:

在单词之间使用“和”一词:

  $to_return = '';
  if (count($components) > 2) {
    $last_item = array_pop($components);
    $to_return = implode(', ', $components);
    $to_return .= ' and ' . $last_item;
  } else {
    $to_return = implode(' and ', $components);
  }

  return $to_return;

给出:

1 hour
32 seconds
1 minute
1 minute and 30 seconds
14 days, 6 hours, 56 minutes and 7 seconds
4 months, 19 days, 21 hours, 21 minutes and 18 seconds
2 years, 9 months, 9 days, 12 hours, 25 minutes and 21 seconds

演示:https://eval.in/822330

希望这会有所帮助:)

答案 1 :(得分:2)

你可以使用这样的东西:

function gramFormat($seconds){

    $dt = new DateTime(gmdate("H:i:s", $seconds));

    //intval is needed to remove leading zeros
    $hour = ($dt->format('H') !=1 ? intval($dt->format('H')) . " hours" : intval($dt->format('H')) . " hour");
    $min = ($dt->format('i') !=1 ? intval($dt->format('i')) . " minutes" : intval($dt->format('i')) . " minute");
    $sec = ($dt->format('s') !=1 ? intval($dt->format('s')) . " seconds" : intval($dt->format('s')) . " second");

    return $hour . " and " . $min . " and " . $sec;
}

echo gramFormat('3601');

输出将是:1 hour and 0 minutes and 1 second

答案 2 :(得分:0)

我不认为有任何预制功能。试试这个:

$time = //time in seconds;
if($time < 60){
  if($time > 1){
     return $time.' seconds';
  } else {
     return $time.' second';
  }
 } else {
   $timestring = '';
   $minutes = floor($time / 60);
   if($minutes > 1){
     $timestring .= $minutes.' Minutes';
   } else {
     $timestring .= $minutes.' Minute';
   }
   $seconds = $time%60;
   if($seconds > 1){
     $timestring .= $seconds.' Seconds';
    } else {
     $timestring .= $seconds.' Second';
    }
    return $timestring;
}