PHP and MySQL Subtract Seconds From Time

2020-03-26 06:27发布

I have 2:00 in a table and I would like to subtract 0:01 from it. I have tried in a recent post before this one, but it is no help.

In PHP and MySQL, how would I subtract 1 second from 2 minutes?

if (isset($_GET['id']) && isset($_GET['time'])) {
    mysql_select_db("aleckaza_pennyauction", $connection);
    $query = "SELECT Current_Time FROM Live_Auctions WHERE ID='1'";
    $results = mysql_query($query) or die(mysql_error());

    while ($row = mysql_fetch_array($results)) {
        $newTime = $row['Current_Time'];
        $query = "INSERT INTO Live_Auctions(Current_Time) VALUES('".$newTime."')";
        $results = mysql_query($query) or die(mysql_error());
    }
}

Thanks.

标签: php mysql
4条回答
小情绪 Triste *
3楼-- · 2020-03-26 07:03

PHP: time():

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

This, -1, into PHP date() should do the trick.

查看更多
▲ chillily
4楼-- · 2020-03-26 07:10

You can do it straight on the DB using MySQL SUBTIME()

// from the manual
mysql> SELECT SUBTIME('2007-12-31 23:59:59.999999','1 1:1:1.000002');
        -> '2007-12-30 22:58:58.999997'

Or you can get the time from MySQL and do it on PHP using DateTime::sub()

 // again from the manual
 <?php 
 $dateA = date_sub('2000-01-20', date_interval_create_from_date_string('1 second')); 
 ?> 
查看更多
趁早两清
5楼-- · 2020-03-26 07:16

Check this code in php flavor, with what u provided-

<?php
$t1= '0:2:00';
$t2= '0:0:01';
$a1 = explode(":",$t1);
$a2 = explode(":",$t2);
$time1 = (($a1[0]*60*60)+($a1[1]*60)+($a1[2]));
$time2 = (($a2[0]*60*60)+($a2[1]*60)+($a2[2]));
$diff = abs($time1-$time2);
$hours = floor($diff/(60*60));
$mins = floor(($diff-($hours*60*60))/(60));
$secs = floor(($diff-(($hours*60*60)+($mins*60))));
$result = $hours.":".$mins.":".$secs;
echo $result;

Screenshot

查看更多
登录 后发表回答