简体   繁体   中英

cant insert the difference of this into table

this is my insert section the $total_minutes and $total_hour cant be inserted in the table

$year = $_POST['year'];  
$month = $_POST['month'];  
$day = $_POST['day'];  
$hour = $_POST['hour'];  
$min = $_POST['min'];  
$sec = $_POST['sec'];  
$year1 = $_POST['year1'];  
$month1 = $_POST['month1'];  
$day1 = $_POST['day1'];  
$hour1 = $_POST['hour1'];  
$min1 = $_POST['min1'];  
$sec1 = $_POST['sec1'];  
$time_in = $year.'-'.$month.'-'.$day.' '.$hour.'-'.$min.'-'.$sec;  
$time_out = $year1.'-'.$month1.'-'.$day1.' '.$hour1.'-'.$min1.'-'.$sec1;
$total_minutes = $total_min;
$total_hour = $total_hr;

$sql = "INSERT INTO time (year, month, day, hour, min, sec, year1, month1, day1, hour1, min1, sec1, time_in, time_out, total_minutes, total_hour)
    VALUES
    ('$year','$month','$day','$hour','$min','$sec','$year1','$month1','$day1','$hour1','$min1','$sec1','$time_in','$time_out', '$total_minutes', '$total_hour')";

how can i add this in my table?


minutes

    $datetime1 = strtotime($row['time_in']); //year-month-day hr:min:sec timein  
$datetime2 = strtotime($row['time_out']); //year-month-day hr:min:sec timeout  
$interval = abs($datetime2 - $datetime1);  
$total_min = round($interval / 60);  
$total_minutes = $total_min;  

hour

function convertToHoursMins($total_minutes, $format = '%02d:%02d') {
    if ($total_minutes < 1) {
        return;
    }
    $hours = floor($total_minutes / 60);
    $wmin = ($total_minutes % 60);
    return sprintf($format, $hours, $wmin);
}
$total_hr = convertToHoursMins($total_minutes, '%02d hours %02d minutes');
$total_hour = $total_hr;

im new and i wanted a simple answer

Use MySQL TIMEDIFF() function to get the answer. Instead of PHP, MySQL Function gives the result in very easiest manner.

$datetime1 = strtotime($row['time_in']); //year-month-day hr:min:sec timein  
$datetime2 = strtotime($row['time_out']); //year-month-day hr:min:sec timeout  

$timeInterval = mysql_query("SELECT TIMEDIFF($datetime2, $datetime1)");

$timeArray = explode(":", $timeInterval);

Hope this may help you :)

You have to convert your datetime1 and datetime2 in this format first :

datetime1 =   2012-01-01   12:00:00;
datetime2 =   2012-01-02   13:00:00;

Then in my sql insert query directly insert value from following functions :

TIMESTAMPDIFF(HOUR, datetime2, datetime1) // For Hours Calculation TIMESTAMPDIFF(MINUTE,datetime2, datetime1) // For Minutes Calculation

Hope this may help you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM