2011-02-05 00:22:15 +01:00
|
|
|
<?php
|
|
|
|
|
2011-04-14 20:17:56 +02:00
|
|
|
class DateHelper
|
2011-02-05 00:22:15 +01:00
|
|
|
{
|
2011-03-22 14:55:33 +01:00
|
|
|
private $_timestamp;
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
2011-02-05 00:22:15 +01:00
|
|
|
$this->_timestamp = date("U");
|
2011-03-22 14:55:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get time of object construction in the format
|
|
|
|
* YYYY-MM-DD HH:mm:ss
|
|
|
|
*/
|
2011-04-14 20:17:56 +02:00
|
|
|
function getTimestamp()
|
2011-03-22 14:55:33 +01:00
|
|
|
{
|
|
|
|
return date("Y-m-d H:i:s", $this->_timestamp);
|
|
|
|
}
|
|
|
|
|
2011-05-31 00:12:57 +02:00
|
|
|
/**
|
|
|
|
* Get date of object construction in the format
|
|
|
|
* YY:mm:dd
|
|
|
|
*/
|
|
|
|
function getDate()
|
|
|
|
{
|
|
|
|
return date("Y-m-d", $this->_timestamp);
|
|
|
|
}
|
|
|
|
|
2011-03-22 14:55:33 +01:00
|
|
|
/**
|
|
|
|
* Get time of object construction in the format
|
|
|
|
* HH:mm:ss
|
|
|
|
*/
|
|
|
|
function getTime()
|
|
|
|
{
|
|
|
|
return date("H:i:s", $this->_timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the internal timestamp of the object.
|
|
|
|
*/
|
|
|
|
function setDate($dateString)
|
|
|
|
{
|
2011-02-08 01:03:14 +01:00
|
|
|
$this->_timestamp = strtotime($dateString);
|
2011-03-22 14:55:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Enter description here ...
|
|
|
|
*/
|
|
|
|
function getNowDayStartDiff()
|
|
|
|
{
|
|
|
|
$dayStartTS = strtotime(date("Y-m-d", $this->_timestamp));
|
|
|
|
return $this->_timestamp - $dayStartTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNowDayEndDiff()
|
|
|
|
{
|
|
|
|
$dayEndTS = strtotime(date("Y-m-d", $this->_timestamp+(86400)));
|
|
|
|
return $dayEndTS - $this->_timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEpochTime()
|
|
|
|
{
|
2011-03-05 05:53:29 +01:00
|
|
|
return $this->_timestamp;
|
2011-03-22 14:55:33 +01:00
|
|
|
}
|
2011-03-05 05:53:29 +01:00
|
|
|
|
2011-03-22 14:55:33 +01:00
|
|
|
public static function TimeDiff($time1, $time2)
|
|
|
|
{
|
2011-03-05 00:26:22 +01:00
|
|
|
return strtotime($time2) - strtotime($time1);
|
2011-03-22 14:55:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function ConvertMSToHHMMSSmm($time)
|
|
|
|
{
|
2011-02-05 00:22:15 +01:00
|
|
|
$hours = floor($time / 3600000);
|
|
|
|
$time -= 3600000*$hours;
|
2011-03-22 14:55:33 +01:00
|
|
|
|
2011-02-05 00:22:15 +01:00
|
|
|
$minutes = floor($time / 60000);
|
|
|
|
$time -= 60000*$minutes;
|
2011-03-22 14:55:33 +01:00
|
|
|
|
2011-02-05 00:22:15 +01:00
|
|
|
$seconds = floor($time / 1000);
|
|
|
|
$time -= 1000*$seconds;
|
2011-03-22 14:55:33 +01:00
|
|
|
|
2011-02-05 00:22:15 +01:00
|
|
|
$ms = $time;
|
2011-03-22 14:55:33 +01:00
|
|
|
|
2011-02-05 00:22:15 +01:00
|
|
|
if (strlen($hours) == 1)
|
2011-03-22 14:55:33 +01:00
|
|
|
$hours = "0".$hours;
|
2011-02-05 00:22:15 +01:00
|
|
|
if (strlen($minutes) == 1)
|
2011-03-22 14:55:33 +01:00
|
|
|
$minutes = "0".$minutes;
|
2011-02-05 00:22:15 +01:00
|
|
|
if (strlen($seconds) == 1)
|
2011-03-22 14:55:33 +01:00
|
|
|
$seconds = "0".$seconds;
|
|
|
|
|
2011-02-05 00:22:15 +01:00
|
|
|
return $hours.":".$minutes.":".$seconds.".".$ms;
|
|
|
|
}
|
2011-05-30 18:24:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function formats a time by removing seconds
|
|
|
|
*
|
|
|
|
* When we receive a time from the database we get the
|
|
|
|
* format "hh:mm:ss". But when dealing with show times, we
|
|
|
|
* do not care about the seconds.
|
|
|
|
*
|
|
|
|
* @param int $p_timestamp
|
|
|
|
* The value which to format.
|
|
|
|
* @return int
|
|
|
|
* The timestamp with the new format "hh:mm", or
|
|
|
|
* the original input parameter, if it does not have
|
|
|
|
* the correct format.
|
|
|
|
*/
|
|
|
|
public static function removeSecondsFromTime($p_timestamp)
|
|
|
|
{
|
|
|
|
//Format is in hh:mm:ss. We want hh:mm
|
|
|
|
$timeExplode = explode(":", $p_timestamp);
|
|
|
|
|
|
|
|
if (count($timeExplode) == 3)
|
|
|
|
return $timeExplode[0].":".$timeExplode[1];
|
|
|
|
else
|
|
|
|
return $p_timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getDateFromTimestamp($p_timestamp){
|
|
|
|
$explode = explode(" ", $p_timestamp);
|
|
|
|
return $explode[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getTimeFromTimestamp($p_timestamp){
|
|
|
|
$explode = explode(" ", $p_timestamp);
|
|
|
|
return $explode[1];
|
|
|
|
}
|
2011-06-17 00:59:15 +02:00
|
|
|
|
|
|
|
/* Given a track length in the format HH:MM:SS.mm, we want to
|
|
|
|
* convert this to seconds. This is useful for Liquidsoap which
|
|
|
|
* likes input parameters give in seconds.
|
|
|
|
* For example, 00:06:31.444, should be converted to 391.444 seconds
|
|
|
|
* @param int $p_time
|
|
|
|
* The time interval in format HH:MM:SS.mm we wish to
|
|
|
|
* convert to seconds.
|
|
|
|
* @return int
|
|
|
|
* The input parameter converted to seconds.
|
|
|
|
*/
|
|
|
|
public static function calculateLengthInSeconds($p_time){
|
|
|
|
|
|
|
|
if (2 !== substr_count($p_time, ":")){
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (1 === substr_count($p_time, ".")){
|
|
|
|
list($hhmmss, $ms) = explode(".", $p_time);
|
|
|
|
} else {
|
|
|
|
$hhmmss = $p_time;
|
|
|
|
$ms = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
list($hours, $minutes, $seconds) = explode(":", $hhmmss);
|
|
|
|
|
|
|
|
$totalSeconds = $hours*3600 + $minutes*60 + $seconds + $ms/1000;
|
|
|
|
|
|
|
|
return $totalSeconds;
|
|
|
|
}
|
2011-02-05 00:22:15 +01:00
|
|
|
}
|
|
|
|
|