2011-02-05 00:22:15 +01:00
|
|
|
<?php
|
|
|
|
|
2012-04-13 23:45:28 +02:00
|
|
|
class Application_Common_DateHelper
|
2011-02-05 00:22:15 +01:00
|
|
|
{
|
2011-08-16 21:04:41 +02:00
|
|
|
private $_dateTime;
|
2011-03-22 14:55:33 +01:00
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
2011-08-16 21:04:41 +02:00
|
|
|
$this->_dateTime = 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
|
|
|
{
|
2011-08-16 21:04:41 +02:00
|
|
|
return date("Y-m-d H:i:s", $this->_dateTime);
|
2011-03-22 14:55:33 +01:00
|
|
|
}
|
2011-11-15 16:32:07 +01:00
|
|
|
|
2011-08-15 22:40:24 +02:00
|
|
|
/**
|
|
|
|
* Get time of object construction in the format
|
|
|
|
* YYYY-MM-DD HH:mm:ss
|
|
|
|
*/
|
|
|
|
function getUtcTimestamp()
|
|
|
|
{
|
2011-11-16 20:06:59 +01:00
|
|
|
return gmdate("Y-m-d H:i:s", $this->_dateTime);
|
2011-08-15 22:40:24 +02:00
|
|
|
}
|
2011-03-22 14:55:33 +01:00
|
|
|
|
2011-05-31 00:12:57 +02:00
|
|
|
/**
|
|
|
|
* Get date of object construction in the format
|
2011-11-22 00:03:56 +01:00
|
|
|
* YYYY-MM-DD
|
2011-05-31 00:12:57 +02:00
|
|
|
*/
|
|
|
|
function getDate()
|
|
|
|
{
|
2011-11-16 20:06:59 +01:00
|
|
|
return gmdate("Y-m-d", $this->_dateTime);
|
2011-05-31 00:12:57 +02:00
|
|
|
}
|
|
|
|
|
2011-03-22 14:55:33 +01:00
|
|
|
/**
|
|
|
|
* Get time of object construction in the format
|
|
|
|
* HH:mm:ss
|
|
|
|
*/
|
|
|
|
function getTime()
|
|
|
|
{
|
2011-11-16 20:06:59 +01:00
|
|
|
return gmdate("H:i:s", $this->_dateTime);
|
2011-03-22 14:55:33 +01:00
|
|
|
}
|
2011-11-22 00:03:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the week start date of this week in the format
|
|
|
|
* YYYY-MM-DD
|
2012-12-03 17:06:56 +01:00
|
|
|
*
|
2011-11-22 00:03:56 +01:00
|
|
|
* @return String - week start date
|
|
|
|
*/
|
|
|
|
function getWeekStartDate()
|
|
|
|
{
|
2012-07-03 19:17:48 +02:00
|
|
|
// our week starts on monday, but php week starts on sunday.
|
|
|
|
$startDate = date('w') == 0 ? date('Y-m-d', strtotime('monday last week')) : date('Y-m-d', strtotime('monday this week'));
|
2011-11-22 00:03:56 +01:00
|
|
|
$startDateTime = new DateTime($startDate);
|
|
|
|
return $startDateTime->format('Y-m-d H:i:s');
|
|
|
|
}
|
2011-03-22 14:55:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the internal timestamp of the object.
|
|
|
|
*/
|
|
|
|
function setDate($dateString)
|
|
|
|
{
|
2011-11-16 20:06:59 +01:00
|
|
|
$dateTime = new DateTime($dateString, new DateTimeZone("UTC"));
|
|
|
|
$this->_dateTime = $dateTime->getTimestamp();
|
2011-03-22 14:55:33 +01:00
|
|
|
}
|
|
|
|
|
CC-2965: Frontend widget displays shows in UTC time
Not only were frontend widgets showing UTC time,
the SQL query was also comparing UTC timestamp with local timestamps,
causing widgets to display shows in the wrong day, etc.
Another problem was that "On air today" widget was simply calling
GetNextShows which returns shows within next 48 hours.
Fixed by:
1. Under models/Show.php:
In the GetCurrentShow/GetNextShows/GetShowsByDayOfWeek functions,
added code to convert UTC timestamp to local timestamp or vice versa,
depending on which one is more suitable, in SQL queries, thus
removing inconsistency in timezones. Also, before returning query result,
added code to convert result to local timezone.
In GetNextShows, added an optional parameter endTime to limit the interval
of shows to get. This is useful for the "On air today" widget.
2. Under models/DateHelper.php:
Added a few timezone functions to help converting timezones easier in Show.php.
3. Under controller/ApiController.php:
Added todayInfoAction which is to be used by "On Air Today" widget.
2011-11-04 21:57:24 +01:00
|
|
|
/**
|
|
|
|
* Calculate and return the timestamp for end of day today
|
|
|
|
* in local time.
|
2011-11-15 16:32:07 +01:00
|
|
|
*
|
CC-2965: Frontend widget displays shows in UTC time
Not only were frontend widgets showing UTC time,
the SQL query was also comparing UTC timestamp with local timestamps,
causing widgets to display shows in the wrong day, etc.
Another problem was that "On air today" widget was simply calling
GetNextShows which returns shows within next 48 hours.
Fixed by:
1. Under models/Show.php:
In the GetCurrentShow/GetNextShows/GetShowsByDayOfWeek functions,
added code to convert UTC timestamp to local timestamp or vice versa,
depending on which one is more suitable, in SQL queries, thus
removing inconsistency in timezones. Also, before returning query result,
added code to convert result to local timezone.
In GetNextShows, added an optional parameter endTime to limit the interval
of shows to get. This is useful for the "On air today" widget.
2. Under models/DateHelper.php:
Added a few timezone functions to help converting timezones easier in Show.php.
3. Under controller/ApiController.php:
Added todayInfoAction which is to be used by "On Air Today" widget.
2011-11-04 21:57:24 +01:00
|
|
|
* For example, if local time is 2PM on 2011-11-01,
|
|
|
|
* then the function would return 2011-11-02 00:00:00
|
2011-11-15 16:32:07 +01:00
|
|
|
*
|
CC-2965: Frontend widget displays shows in UTC time
Not only were frontend widgets showing UTC time,
the SQL query was also comparing UTC timestamp with local timestamps,
causing widgets to display shows in the wrong day, etc.
Another problem was that "On air today" widget was simply calling
GetNextShows which returns shows within next 48 hours.
Fixed by:
1. Under models/Show.php:
In the GetCurrentShow/GetNextShows/GetShowsByDayOfWeek functions,
added code to convert UTC timestamp to local timestamp or vice versa,
depending on which one is more suitable, in SQL queries, thus
removing inconsistency in timezones. Also, before returning query result,
added code to convert result to local timezone.
In GetNextShows, added an optional parameter endTime to limit the interval
of shows to get. This is useful for the "On air today" widget.
2. Under models/DateHelper.php:
Added a few timezone functions to help converting timezones easier in Show.php.
3. Under controller/ApiController.php:
Added todayInfoAction which is to be used by "On Air Today" widget.
2011-11-04 21:57:24 +01:00
|
|
|
* @return End of day timestamp in local timezone
|
|
|
|
*/
|
2011-11-22 00:03:56 +01:00
|
|
|
public static function GetDayEndTimestamp($time = "") {
|
|
|
|
$dateTime = $time == "" ? new DateTime(date("Y-m-d")) : new DateTime($time);
|
CC-2965: Frontend widget displays shows in UTC time
Not only were frontend widgets showing UTC time,
the SQL query was also comparing UTC timestamp with local timestamps,
causing widgets to display shows in the wrong day, etc.
Another problem was that "On air today" widget was simply calling
GetNextShows which returns shows within next 48 hours.
Fixed by:
1. Under models/Show.php:
In the GetCurrentShow/GetNextShows/GetShowsByDayOfWeek functions,
added code to convert UTC timestamp to local timestamp or vice versa,
depending on which one is more suitable, in SQL queries, thus
removing inconsistency in timezones. Also, before returning query result,
added code to convert result to local timezone.
In GetNextShows, added an optional parameter endTime to limit the interval
of shows to get. This is useful for the "On air today" widget.
2. Under models/DateHelper.php:
Added a few timezone functions to help converting timezones easier in Show.php.
3. Under controller/ApiController.php:
Added todayInfoAction which is to be used by "On Air Today" widget.
2011-11-04 21:57:24 +01:00
|
|
|
$dateTime->add(new DateInterval('P1D'));
|
|
|
|
return $dateTime->format('Y-m-d H:i:s');
|
|
|
|
}
|
2011-11-22 00:03:56 +01:00
|
|
|
|
|
|
|
public static function GetDayEndTimestampInUtc($time = "") {
|
2012-04-13 23:45:28 +02:00
|
|
|
$dayEndTimestamp = Application_Common_DateHelper::GetDayEndTimestamp($time);
|
|
|
|
return Application_Common_DateHelper::ConvertToUtcDateTimeString($dayEndTimestamp);
|
2011-11-22 00:03:56 +01:00
|
|
|
}
|
2011-11-15 16:32:07 +01:00
|
|
|
|
2011-03-22 14:55:33 +01:00
|
|
|
/**
|
2011-08-16 21:04:41 +02:00
|
|
|
* Find the epoch timestamp difference from "now" to the beginning of today.
|
2011-03-22 14:55:33 +01:00
|
|
|
*/
|
|
|
|
function getNowDayStartDiff()
|
|
|
|
{
|
2011-08-16 21:04:41 +02:00
|
|
|
$dayStartTs = ((int)($this->_dateTime/86400))*86400;
|
|
|
|
return $this->_dateTime - $dayStartTs;
|
2011-03-22 14:55:33 +01:00
|
|
|
}
|
|
|
|
|
2011-08-16 21:04:41 +02:00
|
|
|
/**
|
|
|
|
* Find the epoch timestamp difference from "now" to the end of today.
|
|
|
|
*/
|
2011-03-22 14:55:33 +01:00
|
|
|
function getNowDayEndDiff()
|
|
|
|
{
|
2011-08-16 21:04:41 +02:00
|
|
|
$dayEndTs = ((int)(($this->_dateTime+86400)/86400))*86400;
|
|
|
|
return $dayEndTs - $this->_dateTime;
|
2011-03-22 14:55:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getEpochTime()
|
|
|
|
{
|
2011-08-16 21:04:41 +02:00
|
|
|
return $this->_dateTime;
|
2011-03-22 14:55:33 +01:00
|
|
|
}
|
2011-03-05 05:53:29 +01:00
|
|
|
|
CC-2965: Frontend widget displays shows in UTC time
Not only were frontend widgets showing UTC time,
the SQL query was also comparing UTC timestamp with local timestamps,
causing widgets to display shows in the wrong day, etc.
Another problem was that "On air today" widget was simply calling
GetNextShows which returns shows within next 48 hours.
Fixed by:
1. Under models/Show.php:
In the GetCurrentShow/GetNextShows/GetShowsByDayOfWeek functions,
added code to convert UTC timestamp to local timestamp or vice versa,
depending on which one is more suitable, in SQL queries, thus
removing inconsistency in timezones. Also, before returning query result,
added code to convert result to local timezone.
In GetNextShows, added an optional parameter endTime to limit the interval
of shows to get. This is useful for the "On air today" widget.
2. Under models/DateHelper.php:
Added a few timezone functions to help converting timezones easier in Show.php.
3. Under controller/ApiController.php:
Added todayInfoAction which is to be used by "On Air Today" widget.
2011-11-04 21:57:24 +01:00
|
|
|
/**
|
|
|
|
* Returns the offset in seconds, between local and UTC timezones.
|
|
|
|
* E.g., if local timezone is -4, this function
|
|
|
|
* returns -14400.
|
2011-11-15 16:32:07 +01:00
|
|
|
*
|
CC-2965: Frontend widget displays shows in UTC time
Not only were frontend widgets showing UTC time,
the SQL query was also comparing UTC timestamp with local timestamps,
causing widgets to display shows in the wrong day, etc.
Another problem was that "On air today" widget was simply calling
GetNextShows which returns shows within next 48 hours.
Fixed by:
1. Under models/Show.php:
In the GetCurrentShow/GetNextShows/GetShowsByDayOfWeek functions,
added code to convert UTC timestamp to local timestamp or vice versa,
depending on which one is more suitable, in SQL queries, thus
removing inconsistency in timezones. Also, before returning query result,
added code to convert result to local timezone.
In GetNextShows, added an optional parameter endTime to limit the interval
of shows to get. This is useful for the "On air today" widget.
2. Under models/DateHelper.php:
Added a few timezone functions to help converting timezones easier in Show.php.
3. Under controller/ApiController.php:
Added todayInfoAction which is to be used by "On Air Today" widget.
2011-11-04 21:57:24 +01:00
|
|
|
* @return type offset in int, between local and UTC timezones
|
|
|
|
*/
|
|
|
|
function getLocalTimeZoneOffset() {
|
|
|
|
$dateTime = new DateTime("@".$this->_dateTime, new DateTimeZone("UTC"));
|
|
|
|
$timezone = new DateTimeZone(date_default_timezone_get());
|
|
|
|
return $timezone->getOffset($dateTime);
|
|
|
|
}
|
2011-11-15 16:32:07 +01:00
|
|
|
|
CC-2965: Frontend widget displays shows in UTC time
Not only were frontend widgets showing UTC time,
the SQL query was also comparing UTC timestamp with local timestamps,
causing widgets to display shows in the wrong day, etc.
Another problem was that "On air today" widget was simply calling
GetNextShows which returns shows within next 48 hours.
Fixed by:
1. Under models/Show.php:
In the GetCurrentShow/GetNextShows/GetShowsByDayOfWeek functions,
added code to convert UTC timestamp to local timestamp or vice versa,
depending on which one is more suitable, in SQL queries, thus
removing inconsistency in timezones. Also, before returning query result,
added code to convert result to local timezone.
In GetNextShows, added an optional parameter endTime to limit the interval
of shows to get. This is useful for the "On air today" widget.
2. Under models/DateHelper.php:
Added a few timezone functions to help converting timezones easier in Show.php.
3. Under controller/ApiController.php:
Added todayInfoAction which is to be used by "On Air Today" widget.
2011-11-04 21:57:24 +01:00
|
|
|
/**
|
|
|
|
* Returns the offset hour in int, between local and UTC timezones.
|
|
|
|
* E.g., if local timezone is -4:30, this function
|
|
|
|
* returns -4.
|
2011-11-15 16:32:07 +01:00
|
|
|
*
|
CC-2965: Frontend widget displays shows in UTC time
Not only were frontend widgets showing UTC time,
the SQL query was also comparing UTC timestamp with local timestamps,
causing widgets to display shows in the wrong day, etc.
Another problem was that "On air today" widget was simply calling
GetNextShows which returns shows within next 48 hours.
Fixed by:
1. Under models/Show.php:
In the GetCurrentShow/GetNextShows/GetShowsByDayOfWeek functions,
added code to convert UTC timestamp to local timestamp or vice versa,
depending on which one is more suitable, in SQL queries, thus
removing inconsistency in timezones. Also, before returning query result,
added code to convert result to local timezone.
In GetNextShows, added an optional parameter endTime to limit the interval
of shows to get. This is useful for the "On air today" widget.
2. Under models/DateHelper.php:
Added a few timezone functions to help converting timezones easier in Show.php.
3. Under controller/ApiController.php:
Added todayInfoAction which is to be used by "On Air Today" widget.
2011-11-04 21:57:24 +01:00
|
|
|
* @return type offset hour in int, between local and UTC timezones
|
|
|
|
*/
|
|
|
|
function getLocalOffsetHour() {
|
|
|
|
$offset = $this->getLocalTimeZoneOffset();
|
|
|
|
return (int)($offset / 3600);
|
|
|
|
}
|
2011-11-15 16:32:07 +01:00
|
|
|
|
CC-2965: Frontend widget displays shows in UTC time
Not only were frontend widgets showing UTC time,
the SQL query was also comparing UTC timestamp with local timestamps,
causing widgets to display shows in the wrong day, etc.
Another problem was that "On air today" widget was simply calling
GetNextShows which returns shows within next 48 hours.
Fixed by:
1. Under models/Show.php:
In the GetCurrentShow/GetNextShows/GetShowsByDayOfWeek functions,
added code to convert UTC timestamp to local timestamp or vice versa,
depending on which one is more suitable, in SQL queries, thus
removing inconsistency in timezones. Also, before returning query result,
added code to convert result to local timezone.
In GetNextShows, added an optional parameter endTime to limit the interval
of shows to get. This is useful for the "On air today" widget.
2. Under models/DateHelper.php:
Added a few timezone functions to help converting timezones easier in Show.php.
3. Under controller/ApiController.php:
Added todayInfoAction which is to be used by "On Air Today" widget.
2011-11-04 21:57:24 +01:00
|
|
|
/**
|
|
|
|
* Returns the offset minute in int, between local and UTC timezones.
|
|
|
|
* E.g., if local timezone is -4:30, this function
|
|
|
|
* returns -30.
|
2011-11-15 16:32:07 +01:00
|
|
|
*
|
CC-2965: Frontend widget displays shows in UTC time
Not only were frontend widgets showing UTC time,
the SQL query was also comparing UTC timestamp with local timestamps,
causing widgets to display shows in the wrong day, etc.
Another problem was that "On air today" widget was simply calling
GetNextShows which returns shows within next 48 hours.
Fixed by:
1. Under models/Show.php:
In the GetCurrentShow/GetNextShows/GetShowsByDayOfWeek functions,
added code to convert UTC timestamp to local timestamp or vice versa,
depending on which one is more suitable, in SQL queries, thus
removing inconsistency in timezones. Also, before returning query result,
added code to convert result to local timezone.
In GetNextShows, added an optional parameter endTime to limit the interval
of shows to get. This is useful for the "On air today" widget.
2. Under models/DateHelper.php:
Added a few timezone functions to help converting timezones easier in Show.php.
3. Under controller/ApiController.php:
Added todayInfoAction which is to be used by "On Air Today" widget.
2011-11-04 21:57:24 +01:00
|
|
|
* @return type offset minute in int, between local and UTC timezones
|
|
|
|
*/
|
|
|
|
function getLocalOffsetMinute() {
|
|
|
|
$offset = $this->getLocalTimeZoneOffset();
|
|
|
|
return (int)(($offset % 3600) / 60);
|
|
|
|
}
|
2011-11-15 16:32:07 +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
|
|
|
}
|
2012-08-02 22:36:12 +02:00
|
|
|
|
|
|
|
public static function TimeAdd($time1, $time2)
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
*
|
2011-08-16 21:04:41 +02:00
|
|
|
* @param int $p_dateTime
|
2011-05-30 18:24:39 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
2011-08-16 21:04:41 +02:00
|
|
|
public static function removeSecondsFromTime($p_dateTime)
|
2011-05-30 18:24:39 +02:00
|
|
|
{
|
|
|
|
//Format is in hh:mm:ss. We want hh:mm
|
2011-08-16 21:04:41 +02:00
|
|
|
$timeExplode = explode(":", $p_dateTime);
|
2011-05-30 18:24:39 +02:00
|
|
|
|
|
|
|
if (count($timeExplode) == 3)
|
|
|
|
return $timeExplode[0].":".$timeExplode[1];
|
|
|
|
else
|
2011-08-16 21:04:41 +02:00
|
|
|
return $p_dateTime;
|
2011-05-30 18:24:39 +02:00
|
|
|
}
|
|
|
|
|
2011-08-16 21:04:41 +02:00
|
|
|
public static function getDateFromTimestamp($p_dateTime){
|
|
|
|
$explode = explode(" ", $p_dateTime);
|
2011-05-30 18:24:39 +02:00
|
|
|
return $explode[0];
|
|
|
|
}
|
|
|
|
|
2011-08-16 21:04:41 +02:00
|
|
|
public static function getTimeFromTimestamp($p_dateTime){
|
|
|
|
$explode = explode(" ", $p_dateTime);
|
2011-05-30 18:24:39 +02:00
|
|
|
return $explode[1];
|
|
|
|
}
|
2011-11-15 16:32:07 +01:00
|
|
|
|
|
|
|
/* Given a track length in the format HH:MM:SS.mm, we want to
|
2011-06-17 00:59:15 +02:00
|
|
|
* convert this to seconds. This is useful for Liquidsoap which
|
2011-11-15 16:32:07 +01:00
|
|
|
* likes input parameters give in seconds.
|
|
|
|
* For example, 00:06:31.444, should be converted to 391.444 seconds
|
2011-06-17 00:59:15 +02:00
|
|
|
* @param int $p_time
|
|
|
|
* The time interval in format HH:MM:SS.mm we wish to
|
|
|
|
* convert to seconds.
|
2011-12-19 21:45:44 +01:00
|
|
|
* @return float
|
2011-06-17 00:59:15 +02:00
|
|
|
* The input parameter converted to seconds.
|
|
|
|
*/
|
|
|
|
public static function calculateLengthInSeconds($p_time){
|
2011-11-15 16:32:07 +01:00
|
|
|
|
2011-06-17 00:59:15 +02:00
|
|
|
if (2 !== substr_count($p_time, ":")){
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-12-03 17:06:56 +01:00
|
|
|
|
2011-06-17 00:59:15 +02:00
|
|
|
if (1 === substr_count($p_time, ".")){
|
|
|
|
list($hhmmss, $ms) = explode(".", $p_time);
|
|
|
|
} else {
|
|
|
|
$hhmmss = $p_time;
|
|
|
|
$ms = 0;
|
|
|
|
}
|
2011-11-15 16:32:07 +01:00
|
|
|
|
2011-06-17 00:59:15 +02:00
|
|
|
list($hours, $minutes, $seconds) = explode(":", $hhmmss);
|
2011-12-19 20:55:23 +01:00
|
|
|
|
|
|
|
// keep ms in 3 digits
|
|
|
|
$ms = substr($ms, 0, 3);
|
|
|
|
|
2011-06-17 00:59:15 +02:00
|
|
|
$totalSeconds = $hours*3600 + $minutes*60 + $seconds + $ms/1000;
|
2011-11-15 16:32:07 +01:00
|
|
|
|
2011-06-17 00:59:15 +02:00
|
|
|
return $totalSeconds;
|
|
|
|
}
|
2011-11-15 16:32:07 +01:00
|
|
|
|
|
|
|
public static function ConvertToUtcDateTime($p_dateString, $timezone=null){
|
|
|
|
if (isset($timezone)) {
|
|
|
|
$dateTime = new DateTime($p_dateString, new DateTimeZone($timezone));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$dateTime = new DateTime($p_dateString, new DateTimeZone(date_default_timezone_get()));
|
|
|
|
}
|
2011-10-24 19:46:16 +02:00
|
|
|
$dateTime->setTimezone(new DateTimeZone("UTC"));
|
2011-11-15 16:32:07 +01:00
|
|
|
|
|
|
|
return $dateTime;
|
2011-10-24 19:46:16 +02:00
|
|
|
}
|
2011-11-15 16:32:07 +01:00
|
|
|
|
2011-10-24 19:46:16 +02:00
|
|
|
public static function ConvertToSpecificTimezoneDateTime($p_dateString, $timezone){
|
|
|
|
$dateTime = new DateTime($p_dateString, new DateTimeZone("UTC"));
|
|
|
|
$dateTime->setTimezone(new DateTimeZone($timezone));
|
|
|
|
|
|
|
|
return $dateTime;
|
|
|
|
}
|
2011-11-15 16:32:07 +01:00
|
|
|
|
2011-08-15 22:40:24 +02:00
|
|
|
public static function ConvertToLocalDateTime($p_dateString){
|
|
|
|
$dateTime = new DateTime($p_dateString, new DateTimeZone("UTC"));
|
|
|
|
$dateTime->setTimezone(new DateTimeZone(date_default_timezone_get()));
|
|
|
|
|
|
|
|
return $dateTime;
|
|
|
|
}
|
|
|
|
|
2012-12-03 17:06:56 +01:00
|
|
|
/* Convenience method to return a date formatted into a String rather than a
|
2011-11-15 21:45:08 +01:00
|
|
|
* DateTime object. Note that if an empty string is provided for $p_dateString
|
2012-12-03 17:06:56 +01:00
|
|
|
* then the current time is provided.
|
|
|
|
*
|
2011-11-15 21:45:08 +01:00
|
|
|
* @param $p_dateString
|
|
|
|
* Date string in UTC timezone.
|
|
|
|
* @param $p_format
|
|
|
|
* Format which the string should be returned in.
|
2012-12-03 17:06:56 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* Date String in localtime
|
2011-11-15 21:45:08 +01:00
|
|
|
* */
|
|
|
|
public static function ConvertToLocalDateTimeString($p_dateString, $p_format="Y-m-d H:i:s"){
|
|
|
|
if (is_null($p_dateString) || strlen($p_dateString) == 0)
|
|
|
|
return $p_dateString;
|
|
|
|
return self::ConvertToLocalDateTime($p_dateString)->format($p_format);
|
2011-08-15 22:40:24 +02:00
|
|
|
}
|
2011-11-22 00:03:56 +01:00
|
|
|
|
|
|
|
public static function ConvertToUtcDateTimeString($p_dateString, $p_format="Y-m-d H:i:s"){
|
|
|
|
if (is_null($p_dateString) || strlen($p_dateString) == 0)
|
|
|
|
return $p_dateString;
|
|
|
|
return self::ConvertToUtcDateTime($p_dateString)->format($p_format);
|
|
|
|
}
|
2012-03-23 17:49:50 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Example input: "00:02:32.746562". Output is a DateInterval object
|
|
|
|
* representing that 2 minute, 32.746562 second interval.
|
2012-12-03 17:06:56 +01:00
|
|
|
*
|
2012-03-23 17:49:50 +01:00
|
|
|
*/
|
|
|
|
public static function getDateIntervalFromString($p_interval){
|
|
|
|
list($hour_min_sec, $subsec) = explode(".", $p_interval);
|
|
|
|
list($hour, $min, $sec) = explode(":", $hour_min_sec);
|
|
|
|
|
|
|
|
return new DateInterval("PT{$hour}H{$min}M{$sec}S");
|
|
|
|
}
|
2012-07-19 22:57:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* returns true or false depending on input is wether in
|
|
|
|
* valid range of SQL date/time
|
|
|
|
* @param string $p_datetime
|
|
|
|
* should be in format of '0000-00-00 00:00:00'
|
|
|
|
*/
|
|
|
|
public static function checkDateTimeRangeForSQL($p_datetime){
|
|
|
|
$info = explode(' ', $p_datetime);
|
|
|
|
$dateInfo = explode('-', $info[0]);
|
2012-08-07 22:17:24 +02:00
|
|
|
if (isset($info[1])) {
|
|
|
|
$timeInfo = explode(':', $info[1]);
|
|
|
|
}
|
|
|
|
$retVal = array();
|
|
|
|
$retVal["success"] = true;
|
2012-07-19 22:57:24 +02:00
|
|
|
|
|
|
|
$year = $dateInfo[0];
|
|
|
|
$month = $dateInfo[1];
|
|
|
|
$day = $dateInfo[2];
|
|
|
|
// if year is < 1753 or > 9999 it's out of range
|
2012-08-07 22:17:24 +02:00
|
|
|
if ($year < 1753) {
|
|
|
|
$retVal['success'] = false;
|
2012-11-15 19:49:44 +01:00
|
|
|
$retVal['errMsg'] = sprintf(_("The year %s must be within the range of 1753 - 9999"), $year);
|
2012-08-07 22:17:24 +02:00
|
|
|
} else if (!checkdate($month, $day, $year)) {
|
|
|
|
$retVal['success'] = false;
|
2012-11-15 19:49:44 +01:00
|
|
|
$retVal['errMsg'] = sprintf(_("%s-%s-%s is not a valid date"), $year, $month, $day);
|
2012-07-19 22:57:24 +02:00
|
|
|
} else {
|
|
|
|
// check time
|
2012-08-07 22:27:18 +02:00
|
|
|
if (isset($timeInfo)) {
|
|
|
|
if (isset($timeInfo[0]) && $timeInfo[0] != "") {
|
|
|
|
$hour = intval($timeInfo[0]);
|
|
|
|
} else {
|
|
|
|
$hour = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($timeInfo[1]) && $timeInfo[1] != "") {
|
|
|
|
$min = intval($timeInfo[1]);
|
|
|
|
} else {
|
|
|
|
$min = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($timeInfo[2]) && $timeInfo[2] != "") {
|
|
|
|
$sec = intval($timeInfo[2]);
|
|
|
|
} else {
|
|
|
|
$sec = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ($hour < 0 || $hour > 23) || ($min < 0 || $min > 59) || ($sec < 0 || $sec > 59) ) {
|
|
|
|
$retVal['success'] = false;
|
2012-11-15 19:49:44 +01:00
|
|
|
$retVal['errMsg'] = sprintf(_("%s:%s:%s is not a valid time"), $timeInfo[0], $timeInfo[1] ,$timeInfo[2]);
|
2012-08-07 22:27:18 +02:00
|
|
|
}
|
2012-07-19 22:57:24 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-07 22:17:24 +02:00
|
|
|
return $retVal;
|
2012-07-19 22:57:24 +02:00
|
|
|
}
|
2012-07-25 18:44:37 +02:00
|
|
|
|
2012-09-17 22:46:51 +02:00
|
|
|
/**
|
|
|
|
* This function is used for calculations! Don't modify for display purposes!
|
|
|
|
*
|
|
|
|
* Convert playlist time value to float seconds
|
|
|
|
*
|
|
|
|
* @param string $plt
|
|
|
|
* playlist interval value (HH:mm:ss.dddddd)
|
|
|
|
* @return int
|
|
|
|
* seconds
|
|
|
|
*/
|
|
|
|
public static function playlistTimeToSeconds($plt)
|
|
|
|
{
|
|
|
|
$arr = preg_split('/:/', $plt);
|
|
|
|
if (isset($arr[2])) {
|
|
|
|
return (intval($arr[0])*60 + intval($arr[1]))*60 + floatval($arr[2]);
|
|
|
|
}
|
|
|
|
if (isset($arr[1])) {
|
|
|
|
return intval($arr[0])*60 + floatval($arr[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return floatval($arr[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is used for calculations! Don't modify for display purposes!
|
|
|
|
*
|
|
|
|
* Convert float seconds value to playlist time format
|
|
|
|
*
|
|
|
|
* @param float $seconds
|
|
|
|
* @return string
|
|
|
|
* interval in playlist time format (HH:mm:ss.d)
|
|
|
|
*/
|
|
|
|
public static function secondsToPlaylistTime($p_seconds)
|
|
|
|
{
|
|
|
|
$info = explode('.', $p_seconds);
|
|
|
|
$seconds = $info[0];
|
|
|
|
if (!isset($info[1])) {
|
|
|
|
$milliStr = 0;
|
|
|
|
} else {
|
|
|
|
$milliStr = $info[1];
|
|
|
|
}
|
|
|
|
$hours = floor($seconds / 3600);
|
|
|
|
$seconds -= $hours * 3600;
|
|
|
|
$minutes = floor($seconds / 60);
|
|
|
|
$seconds -= $minutes * 60;
|
|
|
|
|
|
|
|
$res = sprintf("%02d:%02d:%02d.%s", $hours, $minutes, $seconds, $milliStr);
|
|
|
|
|
|
|
|
return $res;
|
2012-07-25 18:44:37 +02:00
|
|
|
}
|
2011-02-05 00:22:15 +01:00
|
|
|
}
|
|
|
|
|