1-如果任何用户在输入字段中输入 1w2d。
此输入字段中的 2 值应转换为总小时数。
3之后,我必须在数据库中插入这些总小时数。
4当我从数据库中获取总小时数时,它应该返回 1w2d。
可能的表示法是,有一个名为 Estimated time 的输入字段,用户可以在其中输入 1w2d 或 1W2D 。这应该转换为总小时数。之后我必须将其发送到数据库中。
*我感到无助。我不知道该怎么办。我知道如何将周转换为小时,但不知道如何将 1w2d 转换为小时。
1-如果任何用户在输入字段中输入 1w2d。
此输入字段中的 2 值应转换为总小时数。
3之后,我必须在数据库中插入这些总小时数。
4当我从数据库中获取总小时数时,它应该返回 1w2d。
可能的表示法是,有一个名为 Estimated time 的输入字段,用户可以在其中输入 1w2d 或 1W2D 。这应该转换为总小时数。之后我必须将其发送到数据库中。
*我感到无助。我不知道该怎么办。我知道如何将周转换为小时,但不知道如何将 1w2d 转换为小时。
<?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/>";
?>
这是结果: