Page 4 of 4
Re: How do I check hours between two date/times ?
Posted: Wed Jul 09, 2014 8:28 am
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";
}
Re: How do I check hours between two date/times ?
Posted: Wed Jul 09, 2014 8:48 am
by Celauran
Re: How do I check hours between two date/times ?
Posted: Wed Jul 09, 2014 8:55 am
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.
Re: How do I check hours between two date/times ?
Posted: Wed Jul 09, 2014 9:05 am
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
Re: How do I check hours between two date/times ?
Posted: Thu Jul 10, 2014 5:32 am
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?!?!
Re: How do I check hours between two date/times ?
Posted: Thu Jul 10, 2014 5:44 am
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.
Re: How do I check hours between two date/times ?
Posted: Thu Jul 10, 2014 5:47 am
by simonmlewis
Yes sorry, I spotted that myself. I went back to the back I posted (away from here), and added it.