【问题标题】:Convert hours into week,days,hours in laravel 4.2在 laravel 4.2 中将小时转换为周、天、小时
【发布时间】:2015-10-31 18:30:19
【问题描述】:
  1. 用户在输入字段中输入了2w2d2h
  2. 在数据库中显示386
  3. 2w=2*24*7
  4. 2d-2*24
  5. 2h=2
  6. 将以上三个值相加后变为386
  7. 现在我将从数据库中获取 386,它应该显示 2w2d2h

请帮帮我

到目前为止,我已经在转换后插入了值。

preg_match_all('#(\d[wW]+|\d[dD]+|\d[hH]+|\s+)#i', $category, $matches);
$hours = 0;
$days = 0;
$hoursss = 0;
foreach ($matches[0] AS $match) {
    switch(true) {
    case preg_match('/\d[hH]+|\s+/', $match):
        $hours += (int)$match;
        break;

    case preg_match('/\d[dD]+|\s+/', $match):
        $days += (int)$match * 24;
        break;

    case preg_match('/\d[wW]+|s+/', $match):
        $hoursss += (int)$match * 24 * 7;
        break;
    }
    $case = $hours + $hoursss + $days;
}

【问题讨论】:

  • 我注意到您确实已经明确了您的问题。您真正需要帮助的是什么?
  • /\d+[wdh]/i 将是您的正则表达式的简化版本。我不确定你甚至需要为此使用正则表达式;至少不是你现在的程度。

标签: php regex laravel laravel-4 preg-match-all


【解决方案1】:

以下代码会将小时数转换为您需要的表达式:

$hours = 386;
$weeks = floor($hours / (24*7));
$hours -= $weeks * 24 * 7;
$days = floor($hours / 24);
$hours -= $days * 24;

printf("%dw%dd%dh", $weeks, $days, $hours);

【讨论】:

    猜你喜欢
    • 2015-10-30
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2018-02-22
    相关资源
    最近更新 更多