【问题标题】:PHP - Need help with some string functionsPHP - 需要一些字符串函数的帮助
【发布时间】:2010-09-24 01:29:47
【问题描述】:

我正在尝试编写一个脚本来从工作中使用的数据库中提取计划数据。

员工的工作时间每天都存储为这样的字符串

000000000000000000000000000000001111111111111111111111111111111111111111000000000000000000000000

每个字符代表 15 分钟。第一个字符空间是 12:00 AM 第二个是 12:15 AM 等等....

上面的例子中员工的工作时间是 8:00 - 6:00。

我为每个字符位置创建了一个这样的数组。

$time[0] = "12:00";
$time[1] = "12:15";
$time[2] = "12:30";
$time[3] = "12:45";
$time[4] = "1:00";
$time[5] = "1:15";
$time[6] = "1:30";
$time[7] = "1:45";
$time[8] = "2:00";

我可以像这样显示员工时间

echo $time[strpos($string, '1')] . "-" . $time[strpos($string, '0', strpos($string, '1'))];

但如果有人分班,例如 9:30 - 2:00 / 4:00 - 7:00,我无法弄清楚如何进行这项工作

000000000000000000000000000000000000001111111111111111110000000011111111111110000000000000000000

对不起,如果我的英语不好。

谢谢

【问题讨论】:

    标签: php


    【解决方案1】:

    如果您需要处理这样的拆分移位,您不妨在一般情况下解决它,迭代每个字符。

    类似这样的:

    <?PHP
    $date = '2010-09-24'; // or whatever.
    
    $string = '00000000001111100000001111...';
    
    //convert your string in to an array
    $data = explode('',$string); 
    
    $time = strtotime("$date 00:00:00"); //midnight on whatever day $date is
    
    $workstate = 0; // zero means not working, 1 means working.
    foreach($data as $index=>$value){
        // employee started working
        if ($value && ! $workstate){ 
            $state = 1;
            echo date('Y-m-d H:i:s',$time) .'-';
        }
        // employee stopped working
        if ($workstate && ! $value){
            $state = 0;
            echo date('Y-m-d H:i:s',$time) . PHP_EOL;
        }
        $time = strtotime('+15 minutes', $time); increase time by 15 minutes
    }
    

    上面的代码没有经过测试或任何东西,但至少应该说明算法。

    它应该产生一个员工工作时间的列表,每行一个班次。

    编辑:您可能需要处理边缘情况,例如员工的工作时间是 23:45-24:00

    【讨论】:

      【解决方案2】:

      谢谢,修复了几个问题,效果很好!

      <?php
      
      $date = '10/01/2010';
      
      $string = '000000000000000000000000000000000000001111111111111111110000000011111111111110000000000000000000';
      
      //convert your string in to an array
      $data = str_split($string); 
      
      $time = strtotime("$date 00:00:00"); //midnight on whatever day $date is
      
      $workstate = 0; // zero means not working, 1 means working.
      foreach($data as $index=>$value){
          // employee started working
          if ($value && !$workstate){ 
              $workstate = 1;
              echo date('h:i:s',$time) .'-';
          }
          // employee stopped working
          if ($workstate && !$value){
              $workstate = 0;
              echo date('h:i:s',$time) . PHP_EOL;
          }
          $time = strtotime('+15 minutes', $time); #increase time by 15 minutes
      }
      
      ?>
      

      【讨论】:

      • 不客气,但您不应该添加新答案来表示感谢。而是评论我的答案。另外:获得一些声誉,以便您可以投票+接受我的回答:-)
      【解决方案3】:

      这适用于我的两个示例字符串:

      date_default_timezone_set('UTC');
      
      $shift_strings = array(
          '000000000000000000000000000000001111111111111111111111111111111111111111000000000000000000000000',
          '000000000000000000000000000000000000001111111111111111110000000011111111111110000000000000000000'
      );
      
      $initial_time = 0; // 12AM
      $now = 0; // counter
      $increment = 15 * 60; // each digit = 15 minutes, but store as seconds
      $offset = 0;
      foreach ($shift_strings as $string) {
          echo $string, "\n";
      
          $shifts = preg_split('/(0+)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE |PREG_SPLIT_NO_EMPTY);
      
          foreach($shifts as $shift) {
              $start = strftime('%I:%M%P', $now * $increment);
              $now += strlen($shift);
              $end = strftime('%I:%M%P', $now * $increment);
              switch(substr($shift, 0, 1)) {
                  case '0':
                      echo "Offshift $start - $end\n";
                      break;
                  case '1':
                      echo "Onshift  $start - $end\n";
                      break;
                  default:
                      echo "Someone dun goofed, digit is ", $substr($shifts, 0, 1), "?\n";
              }
          }
          echo "\n";
      }
      

      输出是:

      000000000000000000000000000000001111111111111111111111111111111111111111000000000000000000000000
      Offshift 12:00am - 08:00am
      Onshift  08:00am - 06:00pm
      Offshift 06:00pm - 12:00am
      
      000000000000000000000000000000000000001111111111111111110000000011111111111110000000000000000000
      Offshift 12:00am - 09:30am
      Onshift  09:30am - 02:00pm
      Offshift 02:00pm - 04:00pm
      Onshift  04:00pm - 07:15pm
      Offshift 07:15pm - 12:00am
      

      【讨论】:

        最近更新 更多