Scenario: An record was entered into the database.
I am trying to figure out the following equations:
Given these times:
I feel like I'm on the right track. Just need some proper math to do the calculations? I am horrible at math. Any help or guidance would be appreciated. I'm not looking for someone to COMPLETE THIS FOR ME. Just looking for advice on what I'm doing wrong, or how I could do it better. Maybe explain the math formulas necessary to achieve my goal.
Here is what I have so far:
class tools{
public function __construct(){
}
public function check_time($time, $request){
$time = strtotime($time);
if($request == 'since'){
$theTime = time() - $time;
$prefix = 'Since:';
} elseif($request == 'until'){
$midnight = mktime(0, 0, 0, date('n'), date('j'), date('Y'));
$theTime = $midnight - $time;
$prefix = 'Until:';
}
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach($tokens as $unit => $text){
if($theTime < $unit) continue;
$duration = floor($theTime / $unit);
return $prefix.' '.$duration.' '.$text.(($duration>1)?'s':'');
}
}
}// EoF tools class
$tools = new tools();
print_r($tools->check_time('2012-08-22 20:11:20', 'since'));
print_r($tools->check_time('2012-08-22 20:11:20', 'until'));
There are two ways to calculate the total time from the array. Using strtotime() function: The strtotime() function is used to convert string into the time format. This functions returns the time in h:m:s format. Example 1: This example reads the values from the array and converts it into the time format.
00:00 is midnight at the beginning of a day, 24:00 is midnight at the end of a day. For simplicity however, most digital clocks skip 24:00 - declaring that midnight is the start of a new day. I.e. 8th of Feb 24:00 and 9th of Feb 00:00 is in practice the same point in time.
$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);
Using a database querying tool, a midnight timestamp is treated as the start of the date, but in Clarity, it is interpreted as the day before if this timestamp is used for a Finish Date.
The solution here is very simple. There is a minor error that's causing all of your issues.
In your code you have this to calculate midnight.
$midnight = mktime(0, 0, 0, date('n'), date('j'), date('Y'));
This is incorrect for the simple fact that it's using TODAY's midnight (Which would have been 00:00 today, however many hours ago from now. You want midnight TOMORROW, since it's considered 00:00 on 24 hour time and is tomorrow. The correct way to do it is just like this:
$midnight = strtotime("tomorrow 00:00:00");
Just keep in mind that strtotime() bases everything off of GMT, so make sure you set a default timezone in the file/application.
I hope my answer is clear and explains why the code you posted is wrong and how to fix it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With