2010-09-24 60 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