2010-09-24 59 views
1

我正在尝试编写一个脚本来将日程表数据从工作中使用的数据库中提取出来。PHP - 需要一些字符串函数的帮助

工作时间的员工每天被存储为一个字符串,这样

000000000000000000000000000000001111111111111111111111111111111111111111000000000000000000000000 

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

雇员上面的示例工作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 

对不起,如果我的英语很差。

感谢

回答

0

如果您需要处理分割的变化一样,你可能也只是解决它在一般情况下,每个字符迭代。

事情是这样的:

<?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

0

谢谢,一对夫妇的修复和它的作品完美!

<?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 
} 

?> 
+0

欢迎你,但你不应该一个新的答案添加到说谢谢。相反,请评论我的答案。另外:得到一些声誉,所以你可以upvote +接受我的回答:-) – timdev 2010-09-24 03:19:14

0

这对我的作品为你的两个样品字符串:

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