我正在尝试编写一个脚本来从工作中使用的数据库中提取计划数据。
员工的工作时间每天都存储为像这样的字符串
000000000000000000000000000000001111111111111111111111111111111111111111000000000000000000000000
每个角色代表15分钟。第一个角色空间是凌晨12:00,第二个是凌晨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 :(得分: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工作
答案 1 :(得分: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
}
?>
答案 2 :(得分: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