1

I'm trying to subtract the time difference between the current datetime, and a time stated in my database. The datetimes current format is yyyy:mm:dd hh:mm:ss, however for this specific case i just want to subtract the time and not the date so i only want hh:mm:ss to be calculated and then stored into a different variable i can use and format how i want. Is it possible to take a full datetime, break it apart and do a diff on it? I think this is kind of confusing so ask if you need clarification. Here's what i've tried thus far:

<?php
//The time in the database
$classTime = DateTime::createFromFormat('Y-m-d H:i:s', '0000-00-00 18:30:00');

$timein = new DateTime("now", new DateTimeZone('America/Detroit'));
$timein->getTimestamp();
$timeout = $classTime;
$totaltime = $timeout->diff($timein);
$totaltime = $totaltime->format('Y-m-d H:i:s');
$totaltime = date('0000:00:00 H:i:s', strtotime($totaltime));
//I create a new date because i'm storing this time into the database, which can't be done with a datetime.

//FORMAT TIMES
$timein = $timein->format('Y-m-d H:i:s');
$timeout = $timeout->format('Y-m-d H:i:s');
echo "Time in " . $timein . " Time Out " . $timeout . " Total Time " . $totaltime;
?>

This current output returns:

Time in 2013-04-02 14:05:32 Time Out -0001-11-30 18:30:00 Total Time 0000:00:00 00:00:00

But i want it to return something like:

Time in 2013-04-02 14:05:32 Time Out -0000-00-00 18:30:00 Total Time 0000:00:00 03:30:00
4
  • 1
    Are you assuming the time out is always the same date? Why not just included the date? Commented Apr 2, 2013 at 18:10
  • unfortunately it's not, it depends on the class that's pulled from the database, i only did it this way to simply things, sorry if this confused matters more. Commented Apr 2, 2013 at 18:12
  • If you are working with just the 'time' portion, DateTime can't calculate it correctly. Time is based of time stamps with 0 being Jan 1 1970 UTC (if I remember correctly), so anything left after a calculation only using the hours/min/sec of a stamp will give the number of seconds difference, so from there you'd have to calculate that in to hours:min:sec by hand. Commented Apr 2, 2013 at 18:18
  • possible duplicate of PHP find difference between two datetimes Commented Apr 14, 2013 at 14:47

1 Answer 1

1

Your question isn't very clear and I spent quite a bit of time answering the wrong question until I took a careful look at your code to see what you actually wanted.

As far as I understand it you store the finishing time of a lesson in your database as a MySql Datetime type and you want to find the time remaining between now and the time the lesson ends.

I'll ignore timezones for the purpose of this answer.

You start with

$classTime = DateTime::createFromFormat('Y-m-d H:i:s', '0000-00-00 18:30:00');

To do a meaningful time comparison, the date portion of $classTime needs to be set to today:-

$timeIn = new DateTime();

$year = (int)$timeIn->format('Y');
$month  = (int)$timeIn->format('m');
$day = (int)$timeIn->format('d');

$classTime->setDate($year, $month, $day);

You can then do the comparison:-

$diff = $timeIn->diff($classTime);

$diff is now an instance of DateInterval.

We can now echo out the information:-

$start = $timeIn->format('Y-m-d H:i:s');
$end = $classTime->format('Y-m-d H:i:s');
$duration = $diff->format("%Hh, %Im, %Ss");
echo "Time in: $start, Time out: $end, Duration: $duration";

Which at the time I ran the code, gave the following output:-

Time in: 2013-04-05 13:22:54, Time out: 2013-04-05 18:30:00, Duration: 05h, 07m, 06s
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.