Created
September 2, 2021 14:39
-
-
Save miwebguy/021ff88d7356bd55e23061476296d5c6 to your computer and use it in GitHub Desktop.
PHPDateStuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CoreDates | |
{ | |
/** | |
* @brief Return the greater of 2 dates | |
* @param $_first (str date) | |
* @param $_second (str date) | |
* @return (str date) | |
*/ | |
static function dateGreater($_first, $_second) | |
{ | |
$_firstU = strtotime($_first); | |
$_secondU = strtotime($_second); | |
if ($_firstU > $_secondU) return $_first; | |
else return $_second; | |
} | |
/** | |
* @brief Is the first date is greater than the second | |
* @param $_first (str date) | |
* @param $_second (str date) | |
* @return bool | |
*/ | |
static function dateLater($_first, $_second) | |
{ | |
$_first = strtotime($_first); | |
$_second = strtotime($_second); | |
return $_first > $_second; | |
} | |
/** | |
* @brief Is the first date is greater or equal to the second | |
* @param $_first (str date) | |
* @param $_second (str date) | |
* @return bool | |
*/ | |
static function dateLaterEqual($_first, $_second) | |
{ | |
$_first = strtotime($_first); | |
$_second = strtotime($_second); | |
return $_first >= $_second; | |
} | |
/** | |
* @brief Return an array of the days between two dates | |
* @param $_beg (str date) | |
* @param $_end (str date) | |
* @param $_format (str php date format) | |
* @return $_dates (arr list of dates) | |
*/ | |
static function datesBetween($_beg, $_end, $_format = "Y-m-d") | |
{ | |
$_dates = array(); | |
$_beg = strtotime($_beg); | |
$_end = strtotime($_end); | |
while ($_beg <= $_end) { | |
$_dates[] = date($_format, $_beg); | |
$_beg = strtotime("+1 days", $_beg); | |
} | |
return $_dates; | |
} | |
/** | |
* @brief Get the Sunday of a given date's week | |
*/ | |
static function weekBeg($date=false) | |
{ | |
if(!$date) $date = date('Y-m-d'); | |
$day = date('w',strtotime($date)); | |
$first = date('Y-m-d', strtotime('-'.$day.' days') ); | |
return $first; | |
} | |
static function pastMonths() | |
{ | |
$months = array(); | |
for($i=0;$i<=11;$i++){ | |
$month=date('M',strtotime("first day of -$i month")); | |
$monthi=date('m',strtotime("first day of -$i month")); | |
$months[$monthi] = $month ; | |
} | |
return $months; | |
} | |
static function pastYears($limit=20) | |
{ | |
$years = array(); | |
for ($year = date('Y'); $year > date('Y')-20; $year--) $years[]=$year; | |
return $years; | |
} | |
/** | |
* @brief Get the Friday of a given date's week | |
* | |
*/ | |
static function weekEnd($date) | |
{ | |
$day = date('w',strtotime($date)); | |
$start = strtotime($date); | |
$last = date('Y-m-d', strtotime('+'.(6-$day).' days',$start)); | |
return $last; | |
} | |
/** | |
* @brief Convert time to hours after addition | |
*/ | |
static function secondsToHours($number) | |
{ | |
return number_format((($number / 60) / 60), 2); | |
} | |
/** | |
* @brief Measure time between 2 dates | |
*/ | |
static function timeBetween($start_time,$end_time) | |
{ | |
return date("U", strtotime($end_time)) - date("U", strtotime($start_time)); | |
} | |
/** | |
* @brief Measure time between 2 dates, return hours | |
*/ | |
static function hoursBetween($start_time,$end_time) | |
{ | |
$seconds = date("U", strtotime($end_time)) - date("U", strtotime($start_time)); | |
return number_format((($seconds / 60) / 60), 2); | |
} | |
/** | |
* @brief Return an array for starting a bucket for day of week data | |
*/ | |
static function numericWeek() | |
{ | |
$week = array( | |
"0"=>0, | |
"1"=>0, | |
"2"=>0, | |
"3"=>0, | |
"4"=>0, | |
"5"=>0, | |
"6"=>0 | |
); | |
return $week; | |
} | |
/** | |
* @brief Return an array for starting a day of week data array | |
* Used for Template table headers | |
*/ | |
static function alphaWeek() | |
{ | |
$week = array( | |
"0"=>"Sun", | |
"1"=>"Mon", | |
"2"=>"Tue", | |
"3"=>"Wed", | |
"4"=>"Thu", | |
"5"=>"Fri", | |
"6"=>"Sat" | |
); | |
return $week; | |
} | |
/** | |
* @brief Get the previous week | |
*/ | |
static function prevWeek($date) | |
{ | |
return date('Y-m-d', strtotime('-7 day',strtotime($date))); | |
} | |
/** | |
* @brief Get the next week | |
*/ | |
static function nextWeek($date) | |
{ | |
try{ | |
return date('Y-m-d', strtotime('+7 day', strtotime($date))); | |
} catch (Exception $e){ | |
echo $date . " is bad"; | |
} | |
} | |
/** | |
* @brief Get the previous day | |
*/ | |
static function prevDay($date) | |
{ | |
return date('Y-m-d', strtotime('-1 day',strtotime($date))); | |
} | |
/** | |
* @brief Get the next day | |
*/ | |
static function nextDay($date) | |
{ | |
try{ | |
return date('Y-m-d', strtotime('+1 day', strtotime($date))); | |
} catch (Exception $e){ | |
echo $date . " is bad"; | |
} | |
} | |
/** | |
* Check for Valid Date String (YYYY-MM-DD) | |
*/ | |
static function isValidDateString($datestring) | |
{ | |
if(!preg_match("/^[1-2][0-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$datestring) | |
|| date("Y-m-d", strtotime($datestring))!=$datestring) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
/** | |
* Check for Valid Informix DateTime String (YYYY-MM-DD 00:00) | |
*/ | |
static function isValidInformixDateTime($datestring) | |
{ | |
if(!preg_match("/^([1-2][0-9]{3})\-([0-1][0-9])\-([0-3][0-9])([TZ\s]{1})([0-5][0-9])\:([0-5][0-9])$/",$datestring) | |
|| date("Y-m-d h:i", strtotime($datestring))!=$datestring) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
/** | |
* Convert Time From one Zone to Another | |
* | |
* $this->zogi->myAccount['_DEFAULTS']['timeZone']; | |
*/ | |
static function convertTZ($date_time, $from_tz, $to_tz) | |
{ | |
if(is_null($to_tz)) $to_tz = 'EST'; | |
$time_object = new DateTime($date_time, new DateTimeZone($from_tz)); | |
$time_object->setTimezone(new DateTimeZone($to_tz)); | |
return $time_object->format('Y-m-d g:i A T'); | |
} | |
/** | |
* Calculate Weekdays in Month (no holiday logic) | |
* @param date | |
*/ | |
static function weekdaysInMonth($date='') | |
{ | |
//in case no values are passed to the function, use the current month and year | |
if ($date == '') { | |
$y = date('Y'); | |
$m = date('m'); | |
} else { | |
$y = date('Y',strtotime($date)); | |
$m = date('m',strtotime($date)); | |
} | |
$lastday = date("t",mktime(0,0,0,$m,1,$y)); | |
$weekdays = 0; | |
for($d=29; $d<=$lastday; $d++) { | |
$wd = date("w",mktime(0,0,0,$m,$d,$y)); | |
if($wd > 0 && $wd < 6) $weekdays++; | |
} | |
return $weekdays+20; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment