difference between two pair of date and time

Hello,

I was trying to get the difference between two date. Unfortunately I only get whole number and not the formatted as required.
Below is my code which does not work as required.
I get request of 2 instead of 02h30m. Kindly advise what is the issue. Thanks.

[php]
$hd1 = new Datetime(‘2016-10-18 08:30:00’);
$hd2 = new Datetime(‘2016-10-18 12:00:00’);
$HoldInterval = $hd2->diff($hd1);

$d1 = new Datetime(‘2016-10-18 09:00:00’);
$d2 = new Datetime(‘2016-10-18 10:00:00’);
$TactInterval = $d2->diff($d1);
//$interval = $TactInterval - $HoldInterval ); // This is not working…

$dt1 = $TactInterval->format(’%Hh%Im’);
$dt2 = $HoldInterval->format(’%Hh%Im’);

echo $dt1."
";
echo $dt2."
";

$dt3 = $dt2 - $dt1; // This is working but only a whole number shows. it show be 02h30m.

//$dt4 = $dt3->format(’%Hh%Im’); // does not work when formated.

echo $dt3."
";
[/php]

What difference are you looking for? Your format has hours hours minutes minutes.

[php]$TactInterval->format(’%H hours %I minutes ago’);[/php]

Well one problem you have is that
[php]$hd1 = new Datetime(‘2016-10-18 08:30:00’);[/php]

should be written like this

[php]$hd1 = new DateTime(‘2016-10-18 08:30:00’);[/php]

[member=57087]Strider64[/member] , can you explain the difference? It is probably my eyes, I just don’t see a change.

Datetime isn’t DateTime - sorry I don’t know any good eye doctors… ;D

Aye!

thank you for your suggestion but it seems T and t has no difference with the issue.

I need to deduct $HoldInterval from $TactInterval. But it won’t let me.

Well, my IDE netbeans flag Datetime for an error when I ran it on my local server and only corrected it when I had DateTime. I’m pretty good at DateTime for I have used it a lot, just go to my website and you’ll see a calendar that I did. Anyways, I said it was one problem I didn’t say that there weren’t others and it sounds like you don’t have error reporting turn on?

If that is the case turn in by putting the following on top of the page or in a configuration file.
[php]<?php
/* Turn on error reporting */
ini_set(‘display_errors’, 1);
ini_set(‘display_startup_errors’, 1);
error_reporting(-1); // -1 = on || 0 = off[/php]

I’ll also tell you that your treating $TactInterval and $HoldInterval as DateTime Object, it’s a different Object (I using the wrong terminology). However, to get a better understanding what I’m saying do the following.
[php]echo “

” . print_r($HoldInterval, 1) . “
\n”;[/php]
and you’ll see the makeup of the object. This should help you out I think.

Others here like astonecipher and Kevin Rubio will probably help you out better, for I haven’t been too active. Though I do come by here every so often.

What is the date you are trying to get the difference of? You have 4 dates up. So, what are you actually trying to figure out?

Please disregard my first post. I did not remove my remarks that is why it is very confusing. Sorry about that.

[member=57087]Strider64[/member] This echo “

” . print_r($HoldInterval, 1) . “
\n”; is very big help for me when identifying the content of the date.

Please have a look at my code below. I have remove some remarks and fixed it. I just need to somehow deduct the value of $HoldInterval from $TactInterval.

[php]

$hd1 = new Datetime(‘2016-10-18 08:30:00’);
$hd2 = new Datetime(‘2016-10-18 12:00:00’);
$HoldInterval = $hd2->diff($hd1);

$d1 = new Datetime(‘2016-10-18 09:00:00’);
$d2 = new Datetime(‘2016-10-18 10:00:00’);
$TactInterval = $d2->diff($d1);

$dt1 = $TactInterval->format(’%Hh%Im’);
$dt2 = $HoldInterval->format(’%Hh%Im’);

$dt3 = $dt2 - $dt1;

echo $dt2." - “.$dt1.” = “.$dt3;
echo " the answer should be 02h30m but instead I got 2”;

echo “
”;echo “
”;echo “
”;
echo “

” . print_r($HoldInterval, 1) . “
\n”;
[/php]

This can be test in this link
Click here to see the code and run it.

Why are you subtracting 4 datetimes? Trying to figure out a clean way to do this, but it is what you are currently doing that is hindering the understanding.

I think this is what you are after…

[php]$hd1 = new Datetime(‘2016-10-18 08:30:00’);
$hd2 = new Datetime(‘2016-10-18 12:00:00’);
$HoldInterval = $hd2->diff($hd1);

$set[0] = new DateTime("$HoldInterval->h:$HoldInterval->i:00");

$d1 = new Datetime(‘2016-10-18 09:00:00’);
$d2 = new Datetime(‘2016-10-18 10:00:00’);
$TactInterval = $d2->diff($d1);

$set[1] = new DateTime("{$TactInterval->h}:{$TactInterval->i}:00");

$set[2] = $set[0]->diff($set[1]);
echo “The difference is: {$set[2]->h} hours and {$set[2]->i} minutes.”;
[/php]

Returns

The difference is: 2 hours and 30 minutes.

Hello [member=72272]astonecipher[/member]

I am really not good as OOP but your code works as required.

Another question if I need to add accumulate instead of deduct the code will be
[php]
$set[2] = $set[0]->add($set[1]);
[/php]

Is this correct? Thanks in advance.

Not how I would do it, but I still am not sure what you are trying to do!!!

The result on $set[2] will be accumulate to another variable.

Example.
in a loop the result in $set[2] is 1h 30m it will be place in a variable $container so that value of $container will be 1h 30m.
Next loop the value of $set[2] is 2h 30m this will be accumulated or added to $container so that value of $container will be 4h.
and so on.

I would use DateTime::modify to add hours and minutes to the object

Thanks a lot on your suggestion. This is really a big help. However, I tried coding it but I don’t seems to know how to add it to the $accum_value.
See my code below:

[php]$hd1 = new DateTime(‘2016-10-18 08:30:00’);
$hd2 = new DateTime(‘2016-10-18 12:00:00’);
$HoldInterval = $hd2->diff($hd1);

$set[0] = new DateTime("$HoldInterval->h:$HoldInterval->i:00");

$d1 = new DateTime(‘2016-10-18 09:00:00’);
$d2 = new DateTime(‘2016-10-18 10:00:00’);
$TactInterval = $d2->diff($d1);

$set[1] = new DateTime("{$TactInterval->h}:{$TactInterval->i}:00");

$set[2] = $set[0]->diff($set[1]);
echo “The difference is: {$set[2]->h} hours and {$set[2]->i} minutes.”;

$set[3] = new DateTime("{$set[2]->h}:{$set[2]->i}:00");
$accum_value = new DateTime(‘2006-12-12’);
for ($x = 1; $x <= 2; $x++) {
date_modify($accum_value, ‘{$set[3]->h}h {$set[3]->i}m’);
echo “{$accum_value->h}h {$accum_value->i}m”;
}[/php]

This is base on the code example I made.

Hello,

my simple question - would this be easier if using unix timestamp format to work out the difference & then format the difference into Y-m-d H:i:s
Not advising, just asking - i do not understand it well enough to make educated advice :smiley:

Where are these dates coming from? Are they in a database? If so, what is the column type?

thank you for your suggestion. I will note that.

Sponsor our Newsletter | Privacy Policy | Terms of Service