|
Server : Apache/2.4.62 System : FreeBSD fbsdweb2.web.rcn.net 14.1-RELEASE FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC amd64 User : www ( 80) PHP Version : 8.3.8 Disable Function : NONE Directory : /domains/owens.enteract/inc/libs/date/ |
Upload File : |
<?php
/**
* Date Class for handling dates beyond the 1970 and 2038 in both php 4 and 5
* Requires ADODB_Date library file to be in the same path as this library
* Written by Raymond Irving 2008
* This class is licensed under the LGPL.
*
* @version: 0.1
*/
/**
* Creates a Date Class objects that's used to convert and format DateTime values
*/
class DateExt {
var $_timestamp = null;
/**
* Date Class Constructor
* @param $str (Optional) String containing a valid date in the formats:
* Date: dd mmm yyyy,<br />mmm dd yyyy,<br /> mm/dd/yyy,<br /> yyyy/mm/dd,<br /> dd/mm/yyyy. Also supports the delimitors "." and "-". Example: mm-dd-yyyy or mm.dd.yyyy
* Time: hh:mm:ss - Supports the time format that is supported by PHP
*/
function __construct($str=''){
// make sure we have the adodb date libray loaded
if (!function_exists('adodb_mktime')) {
$datelib = dirname(__FILE__).'/adodb-time.inc.php';
if (file_exists($datelib)) {
include_once($datelib);
}
if (!function_exists('adodb_mktime'))
die ('DateExt Class: Unable to load the ADOdb Date Library.');
}
$this->setDate($str);
}
/**
* Sets the Date/Time for the Date object
* @return void
* @param $str String containing a valid date
*/
function setDate($str) {
if (is_numeric($str)) $this->_timestamp = $str;
else {
$this->_timestamp = $this->_makeTimestamp($str);
}
}
// internal function
function _makeTimestamp($str){
$d = ($str && $str!='now') ? $this->parse($str) : getdate();
return adodb_mktime(
$d['hours'],
$d['minutes'],
$d['seconds'],
$d['mon'],$d['mday'],$d['year']);
}
/**
* Returns an ADODB Date timestamp
* @return int
*/
function getTimestamp($date = '') {
return ($date) ? $this->_makeTimestamp($date) : $this->_timestamp;
}
/**
* Format and returns a date string. This function used the PHP date() format.
* @return String
* @param $fmt String
* @param $dtTime Mixed [optional] DateTime String or ADODB Date TimeStamp
*/
public function format($fmt, $dtTime = ''){
$ts = ($dtTime && is_numeric($dtTime)) ? $dtTime : $this->getTimestamp($dtTime);
return adodb_date($fmt,$ts);
}
/**
* Parses a date string and returns an array containing the date parts otherwise false
* It's works great with date values returned from MSSQL, MySQL and others.
* @return Array Returns an array that contains the date parts: year, month, mday, minutes,hours and seconds
* @param $str String Supported Date/Time string format
*/
function parse($str) {
$delim = '';
$dpart = array();
$dt = preg_replace('/(\s)+/',' ',strtolower($str)); // remove extra white spaces
if (strpos($dt,'-') > 0) $delim = '-';
if (strpos($dt,'/') > 0) $delim = '/';
if (!$delim && ($d = strpos($dt,'.'))>0) {
$c = strpos($dt,':');
if (!$c || ($c > $d)) $delim = '.';
}
if ($delim=='-' || $delim=='/' || $delim=='.') {
@list($date,$time) = explode(' ',$dt);
$date = explode($delim,$date);
$date[] = $time;
}
else {
$date = explode(' ',$dt,4);
}
foreach ($date as $i => $v) $date[$i] = trim(trim($v,','));
@list($d1,$d2,$d3,$time) = $date;
$months = array(
'jan','feb','mar','apr','may','jun',
'jul','aug','sep','oct','nov','dec'
);
// get year
if ($d1 > 1000) { $dpart['year'] = $d1; unset($date[0]); }
if ($d3 > 1000) { $dpart['year'] = $d3; unset($date[2]); }
// get month - defaults to mm-dd-yyyy
if (!is_numeric($d1)) for ($i=0; $i<12; $i++) { // mmm dd yyyy
if (strstr($d1,$months[$i])!=false) {
$dpart['mon'] = $i+1;
unset($date[0]);
break;
}
}
else if (!is_numeric($d2)) for($i=0; $i<12; $i++) {
if (strstr($d2,$months[$i])!=false) {
$dpart['mon'] = $i+1;
unset($date[1]);
break;
}
}
else {
if ($d2 <= 12 && $d1 >= 1500) { $dpart['mon'] = $d2; unset($date[1]); } // yyyy-mm-dd
if ($d1 <= 12 && $d3 >= 1500) { $dpart['mon'] = $d1; unset($date[0]); } // mm-dd-yyyy
else if ($d1 > 12 && $d3 >= 1500) { $dpart['mon'] = $d2; unset($date[1]); } // dd-mm-yyyy
}
// get day
unset($date[3]);
$dpart['mday'] = implode('',$date);
if (!is_numeric($dpart['mday'])||$dpart['mday']> 31) return false;
// get time info. use 1 jan 2008 as a starting date
$t = strtotime('1-jan-2008 '.$time);
if($t) {
$t = getdate($t);
$dpart['hours'] = $t['hours'];
$dpart['minutes'] = $t['minutes'];
$dpart['seconds'] = $t['seconds'];
}
return $dpart;
}
}
class DateUtils{
public static function now(){
return date("Y-m-d H:i:s");
}
public static function displayDate($datein,$dateFormatIn = 'Y-m-d'){
if ($datein == "0000-00-00" || $datein == "0000-00-00 00:00:00" || $datein == ""){return "";}
$tempdate = new DateExt($datein);
return $tempdate->format($dateFormatIn);
}
public static function dateDiffYears($date1, $date2){
$diff = strtotime($date2->format("Y-m-d")) - strtotime($date1->format("Y-m-d"));
return floor($diff / (365*60*60*24));
}
public static function dateDiffMonths($date1, $date2){
$diff = strtotime($date2->format("Y-m-d")) - strtotime($date1->format("Y-m-d"));
return floor($diff / (30*60*60*24));
}
public static function dateDiffDays($date1, $date2){
$diff = strtotime($date2->format("Y-m-d")) - strtotime($date1->format("Y-m-d"));
return floor($diff/ (60*60*24));
}
public static function timeSince($date1,$date2=""){
if ($date1 == "0000-00-00" || $date1 == "0000-00-00 00:00:00"){
return "";
}
if ($date2 == ""){
$since = time() - strtotime($date1);
}else{
$since = strtotime($date2) - strtotime($date1);
}
$chunks = array(
array(60 * 60 * 24 * 365 , 'year'),
array(60 * 60 * 24 * 30 , 'month'),
array(60 * 60 * 24 * 7, 'week'),
array(60 * 60 * 24 , 'day'),
array(60 * 60 , 'hour'),
array(60 , 'min'),
array(1 , 'sec')
);
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
if (($count = floor($since / $seconds)) != 0) {
break;
}
}
$print = ($count == 1) ? '1 '.$name : "$count {$name}s";
return $print . " ago";
}
}
/**
* DateRelease Class for handling theatrical and home entertainment release dates
*
* @version: 1.0
EXAMPLE CALLS
$dateReleaseObj = new DateRelease("2013-06-01"); // Overrides 'today' with set date to test
echo $dateReleaseObj->releaseDateMessage("","") . " -- No dates set.<br />";
echo $dateReleaseObj->releaseDateMessage("2010-11-30") . " -- Theatrical Date is two years ago. No HE Date.<br />";
echo $dateReleaseObj->releaseDateMessage("2012-11-30") . " -- Theatrical Date is 6 months ago. No HE Date.<br />";
echo $dateReleaseObj->releaseDateMessage("2013-05-01") . " -- Theatrical Date is a month ago. No HE Date.<br />";
echo $dateReleaseObj->releaseDateMessage("","2013-05-01") . " -- No Theatrical Date. HE Date is a month ago.<br />";
echo $dateReleaseObj->releaseDateMessage("2013-05-31") . " -- Theatrical Date is yesterday. No HE Date.<br />";
echo $dateReleaseObj->releaseDateMessage("2013-06-01") . " -- Theatrical Date is today. No HE Date.<br />";
echo $dateReleaseObj->releaseDateMessage("2013-06-02") . " -- Theatrical Date is tomorrow. No HE Date.<br />";
echo $dateReleaseObj->releaseDateMessage("2013-06-03") . " -- Theatrical Date is following Monday. No HE Date.<br />";
echo $dateReleaseObj->releaseDateMessage("2013-06-07") . " -- Theatrical Date is first upcoming Friday. No HE Date.<br />";
echo $dateReleaseObj->releaseDateMessage("2013-07-01") . " -- Theatrical Date is over a month away. No HE Date.<br />";
echo $dateReleaseObj->releaseDateMessage("2013-10-01") . " -- Theatrical Date is 5 months away. No HE Date.<br />";
echo $dateReleaseObj->releaseDateMessage("2013-12-01","2013-09-01") . " -- Theatrical Date is 7 months away. HE Date is 4 months away. NOTE: If both Release Date and HE Date are set HE can only show after Theatrical Date has passed.<br />";
echo $dateReleaseObj->releaseDateMessage("2014-03-01") . " -- Theatrical Date is 11 months away. No HE Date.<br />";
echo $dateReleaseObj->releaseDateMessage("2014-07-01") . " -- Theatrical Date is 15 months away. No HE Date.<br />";
echo $dateReleaseObj->releaseDateMessage("2013-07-01","2013-08-04") . " -- Theatrical Date is over a month away. HE Date is another month away.<br />";
echo $dateReleaseObj->releaseDateMessage("2013-05-01","2013-06-01") . " -- Theatrical Date is a month ago. HE Date is today.<br />";
echo $dateReleaseObj->releaseDateMessage("2013-05-01","2013-06-02") . " -- Theatrical Date is a month ago. HE Date is tomorrow.<br />";
echo $dateReleaseObj->releaseDateMessage("","2013-07-01") . " -- No Theatrical Date. HE Date is over a month away.<br />";
echo $dateReleaseObj->releaseDateMessage("2014-07-01","") . " -- Theatrical Date is over a year away. No HE Date.<br />";
*/
class DateRelease{
public $dateRelease;
public $dateHE;
public $dateToday;
private $txtInTheatersPrefix = "In Theaters"; //'In Cinemas', 'In Theaters'
private $txtComingSoon = "Coming Soon";
private $txtSpring = "Spring";
private $txtSummer = "Summer";
private $txtFall = "Fall";
private $txtWinter = "Winter";
private $txtToday = "Today";
private $txtTomorrow = "Tomorrow";
private $txtFriday = "Friday";
private $txtNowPlaying = "Now Playing";
private $txtOwnItPrefix = "Own It";
private $txtOwnItNow = "Own It Now";
public function __construct($dateTodayString = '') {
$this->dateToday = new Date($dateTodayString);
}
public function releaseDateMessage($dateReleaseString,$dateHEString = "") {
$this->dateRelease = new Date($dateReleaseString);
$this->dateHE = new Date($dateHEString);
//Determine which dates are set
if (DateUtils::displayDate($dateHEString) != ""){$hasHEDate = true;}
if (DateUtils::displayDate($dateReleaseString) != ""){$hasReleaseDate = true;}
//Are we trying Theatrical, or going straight for HE? Check for blank dates to determine how to proceed.
if ($hasHEDate == true && $hasReleaseDate == false){
return $this->getHEDateString();
}else if ($hasHEDate == false && $hasReleaseDate == true){
return $this->getTheatricalDateString();
}else if ($hasHEDate && $hasReleaseDate){
//SHOW HE (secondary) only if theatrical date (primary) has passed today
if ($this->dateToday->getTimestamp() > $this->dateRelease->getTimestamp()) {
return $this->getHEDateString();
}else{
return $this->getTheatricalDateString();
}
}else{
return $this->txtComingSoon; //release date hasn't been set, and HE date hasn't either
}
}
private function getTheatricalDateString(){
if ($this->dateToday->getTimestamp() < $this->dateRelease->getTimestamp()) {
//pre-release
$yearsUntilRelease = DateUtils::dateDiffYears($this->dateToday, $this->dateRelease);
if ($yearsUntilRelease > 2) return $this->txtComingSoon; //more than two years out
$monthsUntilRelease = DateUtils::dateDiffMonths($this->dateToday, $this->dateRelease);
if ($monthsUntilRelease >= 4) { //More than 4 months out, calculate season of release
$releaseMonth = intval($this->dateRelease->format('n'));
if ($releaseMonth < 3 || $releaseMonth == 12){
//winter
return $this->txtWinter . " " . $this->dateRelease->format('Y');
} else if ($releaseMonth < 6){
//spring
return $this->txtSpring . " " . $this->dateRelease->format('Y');
} else if ($releaseMonth < 9){
//summer
return $this->txtSummer . " " . $this->dateRelease->format('Y');
} else {
//fall
return $this->txtFall . " " .$this->dateRelease->format('Y');
}
}else{
//check for opening tomorrow
$daysUntilRelease = DateUtils::dateDiffDays($this->dateToday, $this->dateRelease);
if ($daysUntilRelease == 1){ //"OPENING TOMORROW" (dateRelease tomorrow and no dateHE)
return $this->txtInTheatersPrefix . " " . $this->txtTomorrow;
}else if ($daysUntilRelease < 7 && $this->dateRelease->format('N') == 5){
return $this->txtInTheatersPrefix . " " . $this->txtFriday;
}else{
return $this->txtInTheatersPrefix . " " . $this->dateRelease->format('n/j'); //"4/20" (dateRelease is within 4 months and no dateHE)
}
}
} else if (strtotime($this->dateToday->format("Y-m-d")) == strtotime($this->dateRelease->format("Y-m-d"))) { //today is release day!
return $this->txtInTheatersPrefix . " " . $this->txtToday;
} else {
$yearsSinceRelease = DateUtils::dateDiffYears($this->dateRelease, $this->dateToday);
if ($yearsSinceRelease >= 2){
return $this->dateRelease->format('Y');//return the year of release (2001)
} else {
return $this->txtNowPlaying; //assume still in theaters
}
}
}
private function getHEDateString(){
$daysUntilHE = DateUtils::dateDiffDays($this->dateToday, $this->dateHE);
if ($daysUntilHE >= 2) {
return $this->txtOwnItPrefix . " " . $this->dateHE->format('n/j'); //near future
} else if ($daysUntilHE == 1) {
return $this->txtOwnItPrefix . " " . $this->txtTomorrow;
} if ($daysUntilHE == 0) {
return $this->txtOwnItPrefix . " " . $this->txtToday;
} else {
return $this->txtOwnItNow;
}
}
}
?>