I want to calculate the times between two set times (Cron job run 1 Hour, every Hour).
E.G:
2:18 PM on 20/06/2012 3:00 PM on 21/06/2012 * Cron Job Runs, More than 24 Hours
How would I calculate this in PHP to do a task that I will develop later.
Script I've attempted:
define("SECONDS_PER_HOUR", 60*60);
date_default_timezone_set('UTC');
$result = mysql_query("SELECT * FROM tickets") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
// Calculate the start time
$str = $row['openTime'] . " " . $row['openDate'];
$startdatetime = strtotime($str);
$enddatetime = time();
// calculate the difference in seconds.
$difference = $enddatetime - $startdatetime;
$hoursDiff = $difference / SECONDS_PER_HOUR;
$minutesDiffRemainder = $difference % SECONDS_PER_HOUR;
echo $row['ticketID'] . ": " . $hoursDiff . "h " . $minutesDiffRemainder . "m<br />";
}
Have you considered actually doing a query that does what you need and not pull all data and doing the comparison in PHP? You could do
SELECT * from TICKETS WHERE DATE_ADD(open time, INTERVAL 24 HOUR) > CURTIME();
From reading your code, I'd assume you need to make the opentime
field is a timestamp, containing both date and time. But that is the sensible choice in most cases, anyway.