douling8087 2015-08-07 09:50
浏览 36
已采纳

如何在laravel 4.2中将周日转换为小时?

1-If any user enter 1w2d in input field.

2-Value in this input field should get converted to total hours.

3After that I have to insert these total hours in database.

4When I will fetch total hours from database it should return 1w2d.

Possible notation are that there is one Input field named as Estimated time where user can enter 1w2d or 1W2D .This should get converted into total hours.After that I have to send it into database.

*I am feeling helpless.I don't know what to do.I know how to convert week into hours but don't have any idea that how to convert 1w2d into hours.

  • 写回答

1条回答 默认 最新

  • dream6120 2015-08-07 11:33
    关注
    <?php
    
    function convertWDHtoHours($data) {
    
      preg_match_all('/(\d+[wW]+|\d+[dD]+|\d+[hH]+)/', $data, $matches);
      $hours = 0;
      foreach($matches[0] AS $match) {
        switch(true) {
          case preg_match('/\d+[hH]+/', $match) :
            $hours += (int)$match;
            break;
    
          case preg_match('/\d+[dD]+/', $match) :
            $hours += (int)$match*24;
            break;
    
          case preg_match('/\d+[wW]+/', $match) :
            $hours += (int)$match*24*7;
            break;
        }
      }
      return $hours;
    }
    
    print "23h => ".convertWDHtoHours('23h')."<br/>";
    print "20d => ".convertWDHtoHours('20d')."<br/>";
    print "2W => ".convertWDHtoHours('2W')."<br/>";
    print "1D2h => ".convertWDHtoHours('1D2h')."<br/>";
    print "1w2H => ".convertWDHtoHours('1w2H')."<br/>";
    print "1w2d => ".convertWDHtoHours('1w2d')."<br/>";
    
    ?>
    

    Here is result:

    http://codepad.viper-7.com/Ioj02x

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振