I have a database that has columns called start date, start time, end date and end time and want to be able to output the difference so it shows the total time spent per project. Below is the code I have but it shows the error PHP Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string (15/08/2021 10:48 AM) at position 0 (1): Unexpected character in view-specific-repair.php:258

On line 258 is the line below

$datetime1 = new DateTime($row->exstdate . ' ' . $row->exstime);

The whole code is below

<?php                                            
$datetime1 = new DateTime($row->exstdate . ' ' . $row->exstime);
$datetime2 = new DateTime($row->exrdate . ' ' . $row->extrime);

$interval = $datetime1->diff($datetime2);?>
Time Spent: <?php echo $interval->format('%Y years, %M months, %D days, %I minutes, %S seconds');?>

I'm not sure what to change it to so it works and outputs the time spent between the start date and start time to the end date and end time

That line 258 may need something like https://newbedev.com/how-to-convert-mysql-timestamp-to-date-time-in-php

As I don't know what your MySQL data looks like all I can do is guess that you'll have to keep $row->exstdate . ' ' . $row->exstime but put that into the example at link.

Maybe?
$new_datetime = DateTime::createFromFormat ( "Y-m-d H:i:s", $row->exstdate . ' ' . $row->exstime);

Nod to PHP usages at https://www.php.net/manual/en/datetime.diff.php

Thank you, will try it that way as read about it online that it could be the format being the issue and needs to be date time format in php

Or we can direct use
$datetime1 = new DateTime(date("Y-m-d H:i:s",strtotime("{$row->exstdate} {$row->exstime}")));

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.