-1

I have two columns in my table column Statestarttime contain timestamp in HH: MM (14:21) format and the Timestamp column contain timestamp in YYYY-MM-DD HH:MM:SS (2019-02-04 14:22:47) format.

I want to find out the difference beetween these columns in HH:MM (00:01)

I'm have written the below code but getting Notice: A non well formed numeric value encountered in the difference column

<?php while($row = mysqli_fetch_array($search_result)):?>
                <tr>
                    <td align="Center"><?php echo $row['id'];?></td>
                    <td align="Center"><?php echo $row['EmpID'];?></td>
                    <td align="Center"><?php echo $row['Date'];?></td>
                    <td align="Center"><?php echo $row['Username'];?></td>
                    <td align="Center"><?php echo $row['Computername'];?></td>
                    <td align="Center"><?php echo $row['State'];?></td>
                    <td align="Center"><?php echo $row['MinutesatState'];?></td>
                    <td align="Center"><?php echo $row['StateStarttime'];?></td>
                    <td align="Center"><?php echo $row['StateEndtime'];?></td>
                    <td align="Center"><?php echo $row['Timestamp'];?></td>
                    <td align="Center"><?php echo $row['StateStarttime']-$row['Timestamp'];?></td>
                </tr>
                <?php endwhile;?>
  • you can get hours and minutes value from date then you can do a subtractions. – Chilll007 Feb 04 '19 at 10:14
  • Possible duplicate of [Subtracting two dates in php](https://stackoverflow.com/questions/10469037/subtracting-two-dates-in-php) – Nico Haase Feb 04 '19 at 10:50
  • if you are looking for a mysql query try this " select timediff(time(str_to_date(Timestamp,"%Y-%m-%d %H:%i:%s")),str_to_date(Statestarttime,"%H:%i:%s")) as time_difference from table_name; – jithin giri Feb 04 '19 at 11:05

1 Answers1

3

You can use this, instead of the hardcoded times pass your times it will work...!

$time1 = date("h:i:s",strtotime("2019-02-04 14:22:47"));
$time2 = date("h:i:s",strtotime("14:21"));

echo date("h", strtotime($time1)-strtotime($time2));
Prashanth Kumar B
  • 566
  • 10
  • 25