How do I check hours between two date/times ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check hours between two date/times ?

Post by simonmlewis »

It's not marking it as a negative if the time is a negative.
Right now it should be on -3, but it's showing as 3.

Code: Select all

$time = new DateTime($row->dateraised);
$time->add(new DateInterval('PT' . $minutes_to_add . 'M'));

$stamp = $time->format('Y-m-d H:i:s');    
echo "$stamp<br/>";
$datetime1 = new DateTime($stamp);
$datetime2 = new DateTime($todaydate);
$interval = $datetime1->diff($datetime2);
$hours   = $interval->format('%h'); 
$minutes = $interval->format('%i');
if ($minutes <= "5" && $minutes > "0") 
{
echo "<font color='#009900'>";
echo 'Call in:  '.($hours * 60 + $minutes);
echo " minutes";
echo "</font>";
}
else if ($minutes == "0") 
{
echo "<font color='#FF0000'><b>Call now</b></font>";
}
else if ($minutes < "0") 
{
echo "<font color='#FF0000'>Time as passed</font>";
}
else
{
echo 'Call in:  '.($hours * 60 + $minutes);
echo " minutes";
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I check hours between two date/times ?

Post by Celauran »

Check the invert property.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check hours between two date/times ?

Post by simonmlewis »

I'm looking but I'm struggling.

Code: Select all

$datetime1 = new DateTime('2009-10-11 14:52:05');
$datetime2 = new DateTime('2009-10-11 14:53:05');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
This works for days with negatives, but not minutes.
Don't see how to make it work for minutes (it will usually be just minutes - if beyond that, I'll prob just block it out.

EDIT:

Code: Select all

// timenow
$datetime1 = new DateTime('2009-10-11 15:41:05');

// call due
$datetime2 = new DateTime('2009-10-11 14:54:05');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%i minutes');
Found this works for minutes, but it's not doing the negative for me.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check hours between two date/times ?

Post by simonmlewis »

FOR REFERENCE:

Sorted it.

Code: Select all

// timenow
$datetime1 = new DateTime('2009-10-11 14:41:05');

// call due
$datetime2 = new DateTime('2009-10-11 14:54:05');
$interval = $datetime1->diff($datetime2);
$timeleft = $interval->format('%r%i minutes');
// assigned to variable to query
      echo "$timeleft";
With additional thanks to this page:
http://www.w3schools.com/php/func_date_ ... format.asp
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check hours between two date/times ?

Post by simonmlewis »

Code: Select all

if ($row->calltime == "ASAP") 
{
$minutes_to_add = 5;
}

if ($row->calltime == "30 minutes") 
{
$minutes_to_add = 30;
}

if ($row->calltime == "1 hour") 
{
$minutes_to_add = 60;
}

$time = new DateTime($row->dateraised);
$time->add(new DateInterval('PT' . $minutes_to_add . 'M'));

$stamp = $time->format('Y-m-d H:i:s'); 

echo date('D, d M Y H:i:s', strtotime($stamp));   
echo "<br/>";

// only show time left if not closed
if ($row->dateclosed == NULL)
{
// timenow
$datetime1 = new DateTime($todaydate);
// call due
$datetime2 = new DateTime($stamp);
$interval = $datetime1->diff($datetime2);
$timeleft = $interval->format('%r%i');
// assigned to variable to query   

echo "<br/>$todaydate<br/>$stamp<br/>$timeleft<br/><br/>";
The above is echoing:
[text]2014-07-10 11:30:19
2014-07-10 10:20:48
-9[/text]
It cannot be -9, as it's over an hour ago?!?!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I check hours between two date/times ?

Post by Celauran »

You asked for the minutes without the hours. Total difference is 1 hour, 9 minutes, 31 seconds. %r%H:%i might be a better choice for format.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I check hours between two date/times ?

Post by simonmlewis »

Yes sorry, I spotted that myself. I went back to the back I posted (away from here), and added it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply