Hi DW, I'm trying to mark attendance register which has the date for which that register is being marked for as well as the date which the register is actually marked on.

For Date as well as Marked Date.

I also have two time picker textboxs which one is for time in and the other is for time out.

Now I've been trying to use even time diff to get time difference between the two times provided but there are issues, basically timediff doesn't seem to work proper it also bring like 18 where it means to bring 8,there's always that first digit which confuses everything.

Another problem is that the register can be marked for anytime meaning it can also be marked for let say 20:00 to 05:00AM the next day morning also it can be marked for 02:00AM to 11:00AM which means the shift started from 2 in the morning to 11 am of the same day and so on.

Basically it can be for anytime at and day and it can even cross days now the problem is also properly calculating how many hours and minutes apart.

Does anyone know a way to tackle this as I've said I've tried using time difference but it's not working as it should.

I am confused when you say the HTML time picker input box can be set to 20:00 for time in and 5:00 for time out, because <input type="time" ...> only allows the end-user to specify a time, not a time and date combination. Should it always be assumed that if the time out is earlier than the time in, that it is the next day? Otherwise, if the time out is later than the time in, should it always be assumed that it is the same day?

Perhaps what you mean to say is that you are using <input type="datetime-local"> which makes a lot more sense, because then the end-user specifies both a date and time together.

Let's assume that the datetime-local control is used and both a start and an end are submitted via an HTTP POST form.

We can now do something such as this:

// Get start and end date/time combos from HTTP form
$start = $_POST['start'];
$end = $_POST['end'];

// Create DateTime objects in PHP for us to work with
$start_object = new date_create($start);
$end_object = new date_create($end);

// Get the difference represented as a PHP object
$diff = date_diff($start, $end);

// $diff is now a PHP object of how many years, months, days, hours, seconds

// See for ourselves what $diff looks like
var_dump($diff);

// Specify a string that says how to format the time interval
$format = '%d days, %h hours, %i minutes';

// Format the difference in a way we want
echo 'The time difference is: ' . $diff->format($format);

You can build the string to format it the way you want from https://www.php.net/manual/en/dateinterval.format.php

Note that every format character must start with a %.

I hope this helps!

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.