sábado, 19 de octubre de 2024

calculate_time_difference

 <?php


function calculate_time_difference($datetime_str) {

    // Split the input string into date and time components

    $datetime_parts = explode(' ', $datetime_str);

    

    // Extract the date and time

    $date_parts = explode('-', $datetime_parts[0]);

    $time_parts = explode(':', $datetime_parts[1]);

    

    // Normalize the date and time into integers for mktime

    $year = (int)$date_parts[0];

    $month = (int)$date_parts[1];

    $day = (int)$date_parts[2];

    

    $hour = (int)$time_parts[0];

    $minute = (int)$time_parts[1];

    $second = (int)$time_parts[2];

    

    // Create the target time using mktime

    $target_time = mktime($hour, $minute, $second, $month, $day, $year);

    

    // Get the current time

    $current_time = time();

    

    // Calculate the difference in seconds

    $diff = $current_time - $target_time;

    

    // Convert seconds into minutes and seconds

    $minutes = floor($diff / 60);    

    $seconds = $diff % 60;

    

    // Output the difference in "minutes:seconds" format

    echo "$minutes:$seconds\n";

    

    // Output the current date and time

    echo date("Y-m-d H:i:s");

}


// Example usage: Pass a date string in the "Y-m-d H:i:s" format

$datetime_str = "2024-10-20 01:45:16";

calculate_time_difference($datetime_str);


?>


No hay comentarios:

Publicar un comentario