私は願っていますが、私はおそらく解決策を考えすぎています。タイムスタンプの差分を出力する
タイムスタンプがdatetimeのmysqlテーブルがあります。これはそれがデータベース(2016-10-08 03:56:26)に現れる方法です。
私は、タイムスタンプと現在の時刻(UTC)との間の差異を示す関数を作成しましたが、これは絶えず増加しています。私のコードを見直してください。現在、それは...いくらか働いています。問題は、タイムスタンプが正午(CST)に作成されたときです。出力は11hrsが経過していないと言いますが、これは真ではありません。すべての助けに感謝します。
<?php
// Set timezone
date_default_timezone_set("America/Chicago");
// Time format is UNIX timestamp or
// PHP strtotime compatible strings
function dateDiff($time1, $time2, $precision = 6) {
// If not numeric then convert texts to unix timestamps
if (!is_int($time1)) {
$time1 = strtotime($time1);
}
if (!is_int($time2)) {
$time2 = strtotime($time2);
}
// If time1 is bigger than time2
// Then swap time1 and time2
if ($time1 > $time2) {
$ttime = $time1;
$time1 = $time2;
$time2 = $ttime;
}
// Set up intervals and diffs arrays
$intervals = array('year','month','day','hour','minute','second');
$diffs = array();
// Loop thru all intervals
foreach ($intervals as $interval) {
// Create temp time from time1 and interval
$ttime = strtotime('+1 ' . $interval, $time1);
// Set initial values
$add = 1;
$looped = 0;
// Loop until temp time is smaller than time2
while ($time2 >= $ttime) {
// Create new temp time from time1 and interval
$add++;
$ttime = strtotime("+" . $add . " " . $interval, $time1);
$looped++;
}
$time1 = strtotime("+" . $looped . " " . $interval, $time1);
$diffs[$interval] = $looped;
}
$count = 0;
$times = array();
// Loop thru all diffs
foreach ($diffs as $interval => $value) {
// Break if we have needed precission
if ($count >= $precision) {
break;
}
// Add value and interval
// if value is bigger than 0
if ($value > 0) {
// Add s if value is not 1
if ($value != 1) {
$interval .= "s";
}
// Add value and interval to times array
$times[] = $value . " " . $interval;
$count++;
}
}
// Return string with times
return implode(" ", $times);
}
?>
マニュアルを参照すると助けになると思います。私は明日あなたと確認します。ありがとう。 –
私はこれがまだ動作していないことを知っています。 date_default_timezone_setを( "America/Chicago")に編集しました。タイムスタンプが** 2016-10-09 12:29:05 **のアイテムが1つあります。出力される* difference *は** 2です時間32分40秒**。助けてください。誰でも。 –