我已经构建了一个需要几秒钟的计算脚本,并在包含标签等的 y:d:h:m:s 中显示它们。
例如。15768012
=1 year 12 seconds
和27362
= 15 hours 6 minutes 2 seconds
。
该脚本运行良好,但我想查看字符串并将其除以最后一个数字(不是第一个数字),然后插入单词and
,以便:
15768012
=1 year AND 12 seconds
和27362
= 15 hours 6 minutes AND 2 seconds
。
如果您需要,我可以编写我的脚本,但我认为它不会帮助您解决这个问题..
假设$string = '15 hours 6 minutes 2 seconds'
您将如何拆分它并导入文本and
?
<?
if (isset($_POST['number'])) {
$x = $_POST['number'];
} else {
$x = 54098;
}
// Labels
$lb_y = 'year';
$lb_ys = 'years';
$lb_d = 'day';
$lb_ds = 'days';
$lb_h = 'hour';
$lb_hs = 'hours';
$lb_m = 'minute';
$lb_ms = 'minutes';
$lb_s = 'second';
$lb_ss = 'seconds';
$lb_and = 'and';
//$x = 54098; // Time in seconds - change to $row['time'];
$f = 15768000; // seconds in a year
$d = 43200; // seconds in a day
$t = 3600; // seconds in an hour
$m = 60; // senconds in a minute
// Let's check if it is more than a minute
if ($x >= $m) {
// More than a minute
// Let's check if it is more than an hour
if ($x >= $t) {
// More than an hour
// Let's check if it is more than a day
if($x >= $d) {
// More than a day
// Let's check if it is more than a year
if ($x >= $f) {
// More than a year
// Calculate years
$a = $x / $f;
// Get everything before the separator '.'
if (false !== ($cal = strpos($a, '.'))) {
$a = substr($a, 0, $cal);
}
// $k = what's left
$k = $x - ($f * $a);
// Calculate days
$b = $k / $d;
// Get everything before the separator '.'
if (false !== ($cal = strpos($b, '.'))) {
$b = substr($b, 0, $cal);
}
// $y = what's left
$y = $k - ($d * $b);
// Calculate hours
$c = $y / $t;
// Get everything before the separator '.'
if (false !== ($cal = strpos($c, '.'))) {
$c = substr($c, 0, $cal);
}
// $z = what's left
$z = $y - ($t * $c);
// Calculate minutes
$e = $z / $m;
// Get everything before the separator '.'
if (false !== ($cal = strpos($e, '.'))) {
$e = substr($e, 0, $cal);
}
// $q = what's left
$q = $z - ($m * $e);
// Rewrite numbers if below 9
if ($a <= 9) { $xa = '0'.$a; } else { $xa = $a; }
if ($b <= 9) { $xb = '0'.$b; } else { $xb = $b; }
if ($c <= 9) { $xc = '0'.$c; } else { $xc = $c; }
if ($e <= 9) { $xe = '0'.$e; } else { $xe = $e; }
if ($q <= 9) { $xq = '0'.$q; } else { $xq = $q; }
// Rewrite labels
if ($a <= 1) { $lb_ys = $lb_y; }
if ($b <= 1) { $lb_ds = $lb_d; }
if ($c <= 1) { $lb_hs = $lb_h; }
if ($e <= 1) { $lb_ms = $lb_m; }
if ($q <= 1) { $lb_ss = $lb_s; }
// if == 0 - do not show
$a = $a.' '.$lb_ys.' ';
if ($b == 0) {$b = '';} else {$b = $b.' '.$lb_ds.' ';}
if ($c == 0) {$c = '';} else {$c = $c.' '.$lb_hs.' ';}
if ($e == 0) {$e = '';} else {$e = $e.' '.$lb_ms.' ';}
if ($q == 0) {$q = '';} else {$q = $q.' '.$lb_ss;}
echo $xa.':'.$xb.':'.$xc.':'.$xe.':'.$xq;
echo '<br>'.$a.$b.$c.$e.$q;
} else {
// Less than a year - but more than one day
// Calculate days
$b = $x / $d;
// Get everything before the separator '.'
if (false !== ($cal = strpos($b, '.'))) {
$b = substr($b, 0, $cal);
}
// $y = what's left
$y = $x - ($d * $b);
// Calculate hours
$c = $y / $t;
// Get everything before the separator '.'
if (false !== ($cal = strpos($c, '.'))) {
$c = substr($c, 0, $cal);
}
// $z = what's left
$z = $y - ($t * $c);
// Calculate minutes
$e = $z / $m;
// Get everything before the separator '.'
if (false !== ($cal = strpos($e, '.'))) {
$e = substr($e, 0, $cal);
}
// $q = what's left
$q = $z - ($m * $e);
// Rewrite numbers if below 9
if ($b <= 9) { $xb = '0'.$b; } else { $xb = $b; }
if ($c <= 9) { $xc = '0'.$c; } else { $xc = $c; }
if ($e <= 9) { $xe = '0'.$e; } else { $xe = $e; }
if ($q <= 9) { $xq = '0'.$q; } else { $xq = $q; }
// Rewrite labels
if ($b <= 1) { $lb_ds = $lb_d; }
if ($c <= 1) { $lb_hs = $lb_h; }
if ($e <= 1) { $lb_ms = $lb_m; }
if ($q <= 1) { $lb_ss = $lb_s; }
// if == 0 - do not show
$b = $b.' '.$lb_ds.' ';
if ($c == 0) {$c = '';} else {$c = $c.' '.$lb_hs.' ';}
if ($e == 0) {$e = '';} else {$e = $e.' '.$lb_ms.' ';}
if ($q == 0) {$q = '';} else {$q = $q.' '.$lb_ss;}
echo $xb.':'.$xc.':'.$xe.':'.$xq;
echo '<br>'.$b.$c.$e.$q;
}
} else {
// Less than a day
// Calculate hours
$c = $x / $t;
// Get everything before the separator '.'
if (false !== ($cal = strpos($c, '.'))) {
$c = substr($c, 0, $cal);
}
// $z = what's left
$z = $x - ($t * $c);
// Calculate minutes
$e = $z / $m;
// Get everything before the separator '.'
if (false !== ($cal = strpos($e, '.'))) {
$e = substr($e, 0, $cal);
}
// $q = what's left
$q = $z - ($m * $e);
// Rewrite numbers if below 9
if ($c <= 9) { $xc = '0'.$c; } else { $xc = $c; }
if ($e <= 9) { $xe = '0'.$e; } else { $xe = $e; }
if ($q <= 9) { $xq = '0'.$q; } else { $xq = $q; }
// Rewrite labels
if ($c <= 1) { $lb_hs = $lb_h; }
if ($e <= 1) { $lb_ms = $lb_m; }
if ($q <= 1) { $lb_ss = $lb_s; }
// if == 0 - do not show
$c = $c.' '.$lb_hs.' ';
if ($e == 0) {$e = '';} else {$e = $e.' '.$lb_ms.' ';}
if ($q == 0) {$q = '';} else {$q = $q.' '.$lb_ss;}
echo $xc.':'.$xe.':'.$xq;
echo '<br>'.$c.$e.$q;
}
} else {
// Less than an hour
// Calculate minutes
$e = $x / $m;
// Get everything before the separator '.'
if (false !== ($cal = strpos($e, '.'))) {
$e = substr($e, 0, $cal);
}
// $q = what's left
$q = $x - ($m * $e);
// Rewrite numbers if below 9
if ($e <= 9) { $xe = '0'.$e; } else { $xe = $e; }
if ($q <= 9) { $xq = '0'.$q; } else { $xq = $q; }
// Rewrite labels
if ($e <= 1) { $lb_ms = $lb_m; }
if ($q <= 1) { $lb_ss = $lb_s; }
// if == 0 - do not show
$e = $e.' '.$lb_ms.' ';
if ($q == 0) {$q = '';} else {$q = $q.' '.$lb_ss;}
echo $xe.':'.$xq;
echo '<br>'.$e.$q;
}
} else {
// Less than a minute
// Only secounds
// Rewrite numbers if below 9
if ($x <= 9) { $xx = '0'.$x; } else { $xx = $x; }
// Rewrite labels
if ($x <= 1) { $lb_ss = $lb_s; }
// if == 0 - do not show
$x = $x.' '.$lb_ss;
echo '00:'.$xx;
echo '<br>'.$x;
}
?>
<form action="" method="post"><input name="number" type="text"><input name="" type="submit" value="Submit"></form>
谢谢大家的帮助..这是简化和完成的脚本..不过我有一个新问题..当您在该字段中输入少于时1576
,它会写一些与我想要的不同的数字...
编辑:问题现已解决。欢迎使用脚本:D
<?
if (isset($_POST['number'])) {
$x = $_POST['number'];
} else {
$x = 54098;
}
// Labels
$lb_y = 'year';
$lb_ys = 'years';
$lb_d = 'day';
$lb_ds = 'days';
$lb_h = 'hour';
$lb_hs = 'hours';
$lb_m = 'minute';
$lb_ms = 'minutes';
$lb_s = 'second';
$lb_ss = 'seconds';
$lb_and = 'and';
//$x = 54098; // Time in seconds - change to $row['time'];
$f = 15768000; // seconds in a year
$d = 43200; // seconds in a day
$h = 3600; // seconds in an hour
$m = 60; // seconds in a minute
$a = $x / $f;
if (false !== ($cal = strpos($a, 'E-'))) { $a = 0; }
if (false !== ($cal = strpos($a, '.'))) { $a = substr($a, 0, $cal); }
if ($a <= 0) { $a = 0; }
$b = ($x - ($f * $a)) / $d;
if (false !== ($cal = strpos($b, 'E-'))) { $b = 0; }
if (false !== ($cal = strpos($b, '.'))) { $b = substr($b, 0, $cal); }
if ($b <= 0) { $b = 0; }
$c = ($x - ($f * $a) - ($d * $b)) / $h;
if (false !== ($cal = strpos($c, 'E-'))) { $c = 0; }
if (false !== ($cal = strpos($c, '.'))) { $c = substr($c, 0, $cal); }
if ($c <= 0) { $c = 0; }
$e = ($x - ($f * $a) - ($d * $b) - ($h * $c)) / $m;
if (false !== ($cal = strpos($e, '.'))) { $e = substr($e, 0, $cal); }
if ($e <= 0) { $e = 0; }
$q = ($x - ($f * $a) - ($d * $b) - ($h * $c) - ($m * $e));
// Rewrite numbers if below 9
if ($a <= 9) { $xa = '0'.$a; } else { $xa = $a; }
if ($b <= 9) { $xb = '0'.$b; } else { $xb = $b; }
if ($c <= 9) { $xc = '0'.$c; } else { $xc = $c; }
if ($e <= 9) { $xe = '0'.$e; } else { $xe = $e; }
if ($q <= 9) { $xq = '0'.$q; } else { $xq = $q; }
// Rewrite labels
if ($a <= 1) { $lb_ys = $lb_y; }
if ($b <= 1) { $lb_ds = $lb_d; }
if ($c <= 1) { $lb_hs = $lb_h; }
if ($e <= 1) { $lb_ms = $lb_m; }
if ($q <= 1) { $lb_ss = $lb_s; }
// if == 0 - do not show
if ($a == 0) {$a = '';} else {$a = $a.' '.$lb_ys;}
if ($b == 0) {$b = '';} else {$b = $b.' '.$lb_ds;}
if ($c == 0) {$c = '';} else {$c = $c.' '.$lb_hs;}
if ($e == 0) {$e = '';} else {$e = $e.' '.$lb_ms;}
if ($q == 0) {$q = '';} else {$q = $q.' '.$lb_ss;}
echo $xa.':'.$xb.':'.$xc.':'.$xe.':'.$xq.'<br>';
$time = array($a, $b, $c, $e, $q);
$time = array_filter($time);
$count = count($time);
$last = array_pop($time);
if ($count == 1) {
$string = $last;
} elseif ($count == 0) {
$string = '<i>No Time described</i>';
} else {
$string = implode(', ', $time) . ' '.$lb_and.' ' . $last;
}
echo '<br>'.$string;
?>
<br><br>
<form action="" method="post"><input name="number" type="text"><input name="" type="submit" value="Submit"></form>