数秒かかる計算スクリプトを作成し、ラベルなどを含む 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>