<?php
$visualDNATimeObj = new VisualDNATime();
$time1 = 1800000000;
$time2 = 1800000000;
$numOfMinDifference = $visualDNATimeObj->getTimeDifferenceInMinutes($time1, $time2);
$visualDNATimeObj->showTimeDifferenceInMinutes($time1, $time2);
/**
* VisualDNATime class is used to manage timing functions for VisualDNA
*
* @author Stefano Margelli
*
*/
class VisualDNATime {
/**
* It returns the number of minutes between two timestamps $time1 and $time2
*
* @param var $time1
* @param var $time2
*
* @return int
*
*/
public function getTimeDifferenceInMinutes($time1, $time2){
//Control if both time are valid timestamps
if(!($this->isTimestamp($time1) and $this->isTimestamp($time2))){
return 0;
}
if($time1 == $time2) return 0;
$timestampDifference = $time1 - $time2;
$numOfMinutes = (int)($timestampDifference/60);
/**
* To allow the function showing a positive difference number of seconds
* even if $time2 is a bigger value than $time1
*/
if($numOfMinutes < 0) $numOfMinutes = $numOfMinutes * -1;
return $numOfMinutes;
}
/**
* It prints the number of minutes between two timestamps $time1 and $time2
*
* @param var $time1
* @param var $time2
*
* @return void
*
*/
public function showTimeDifferenceInMinutes($time1, $time2){
//Control if both time are valid timestamps
if(!($this->isTimestamp($time1) and $this->isTimestamp($time2))){
echo "Exception: no valid timestamps have been passed to the function as parameters";
return;
}
//Set default time zone
date_default_timezone_set('Europe/Dublin');
//Format date to RFC 2822
$date1Text = date('r',$time1);
$date2Text = date('r',$time2);
$numOfMinutes = $this->getTimeDifferenceInMinutes($time1, $time2);
//print number of minutes between the dates
echo "Number of minutes between \"$date1Text\" and \"$date2Text\" is: $numOfMinutes";
}
/**
* It check if the value $timestamp is a valid timestamp
*
* @param var $timestamp
*
* @return boolean
*
*/
public function isTimestamp($timestamp){
if(is_numeric($timestamp) && (int)$timestamp == $timestamp){
return true;
}
return false;
}
}
?>