一起添加秒

时间:2015-04-10 16:43:42

标签: php html mysql arrays

我试图像这样添加秒

$h = str_replace("h ",":","$totalTime");
$m = str_replace("m ",":","$h");
$all = str_replace("s","","$m");

$parsed = date_parse($all);
$seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];

echo $seconds;

结果就像这样

12766 0 4460 0 9219 0 0

我觉得这很好,因为有七个结果12766,0,4460,0,9219,0,0 我想添加它们但请记住它们是数组的符号。 我一直在尝试这个$ seconds * 7;或$秒+秒+秒+秒+秒+秒+秒但我得到这个

89362 0 31220 0 64533 0 0

有没有办法将它们全部添加起来并获得秒值?

如3294242

这是所有代码

$query = "SELECT date, start, end, day_total, TIMESTAMPDIFF(SECOND, start, end) as day_t
          FROM `timesheet`
          WHERE `date` >= CAST('$start' AS DATE)
            AND `date` <= DATE_ADD(CAST('$start' AS DATE), INTERVAL 6 DAY) AND e_user='$username'";
$result = mysqli_query($db, $query);

//Dump results into an array
$records = array();
while($row = mysqli_fetch_assoc($result))
{
    $records[$row['date']] = $row;
}

//Create a loop from first to last date
//for each day, check if there is a matching records
//in the records array. If so, use that data
//Else, there was no data for that date

for($day=0; $day<7; $day++)
{
    $timeStamp = strtotime("{$start} +{$day} day");
    $dateStamp = date("Y-m-d", $timeStamp);
    $DOW       = date("D", $timeStamp);
    $dateStr   = date("m/d/Y", $timeStamp);

    //Set default values
    $startTime = '';
    $endTime = '';
    $totalHours = '';
    if(isset($records[$dateStamp]))
    {
        $startTime = $records[$dateStamp]['start'];
        $endTime   = $records[$dateStamp]['end'];
        $totalTimeer = $records[$dateStamp]['day_t'];
    } 
        // $totalTime = $records[$dateStamp]['day_total'];
        $totalTime = $records[$dateStamp]['day_total'];

$h = str_replace("h ",":","$totalTime");
$m = str_replace("m ",":","$h");
$all = str_replace("s","","$m");

$parsed = date_parse($all);
$seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];
$seconds += $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];
echo $seconds;

1 个答案:

答案 0 :(得分:1)

你的for循环会一个接一个地回显秒,而你的$seconds = $parsed['hour']位每次都会重置它。可能的解决方法:

$seconds = 0;
for($day=0; $day<7; $day++)
{
   ...
   $seconds += $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];
}
echo $seconds;