Most of us, programmer have conditions of calculating the difference between timestamps, i.e the difference between now and the timestamp from mysql result. The logic for this is pretty simple. Just convert the two timestamps to Unix time format and take the difference between the two. Next convert the difference into corresponding days,hour,minutes or in any unit you want to.
Here’s the source code for finding out the difference between two timestamps.
<?php
function expiredTime($expdate){
$temp1=explode(" ",$expdate);
$temp=explode("-",$temp1[0]);
$year = $temp[0];
$month= $temp[1];
$day = $temp[2];
$hour = '00';
$minute = '00';
$second = '00';
list($dl,$hl,$ml,$sl) = countdown($year, $month, $day, $hour,$minute, $second);
return $dl."days ". $hl."hr ".$ml."min ".$sl."sec";
}
?>
<?php
function countdown($year, $month, $day, $hour, $minute, $second)
{
global $return;
global $countdown_date;
$countdown_date = mktime($hour, $minute, $second, $month, $day, $year);
$today = time();
$diff = $countdown_date - $today;
if ($diff < 0)$diff = 0;
$dl = floor($diff/60/60/24);
$hl = floor(($diff - $dl*60*60*24)/60/60);
$ml = floor(($diff - $dl*60*60*24 - $hl*60*60)/60);
$sl = floor(($diff - $dl*60*60*24 - $hl*60*60 - $ml*60));
// OUTPUT
$return = array($dl, $hl, $ml, $sl);
return $return;
}
?>
There are two functions here expiredTime ( ) and countdown ( ). The input for the expiredTime is the timestamp from the mysql result. This function evaluates the value of year,month,hour,minutes and seconds of the input parameter and then calls the next function countdown ( ), with those evaluated values as inputs.
The countdown function then calculates the time now and the difference code block 2 #08. Then the difference is divided accordingly, with appropriate denominators to find out the days,hour,minutes,seconds passed the input time, untill now. Code block 2 #10-13 is simple mathematics. This function then returns the difference value in days,hour,minutes and seconds.
Nice job…thanx for the code..
pretty nice job calculating time difference. i will be expecting some more nice job like this from you.
but one suggestion regarding this code, i think this code need more enhancement. i suggest you to give a look at php function strtotime().
well as it is said “there is more than one way to do it”..
good luck.
I have a problem to be resolved and i am new in IT education. Please guide me how can i solve it. Following is the phrase:
(Time Stamp, Value)
$data = Array(
Array(1230786000, 5),
Array(1230786100, 2),
Array(1230786200, 4),
Array(1230786300, 30),
Array(1230786400, 55),
Array(1230786500, 75),
Array(1230786600, 77)
)
Averaged over 300 seconds or 5 minutes would be:
1230786600 => 8:05 5.8
1230786500 => 8:10 6.6
Averaged over 600 seconds or 10 minutes would be:
8:10 6.2
When there are “holes” in the data and there are intervals with no qualifying data points, the resulting array should not hold no data point as well.
I will gather some test data for you. I will have this data ready for your testing tomorrow afternoon.
function data_average($data_points, $interval_in_seconds)sh
{
// Parse array, get the staring time range.
// Loop through time range, stepping averaging each time interval
// return result_array;
@shwan..I couldn’t get what you are looking for..is it the average values for the key of that array to that maps to the value of that error..or what… plz do provide some more info..:D
nice.
quite helpful