Last active
April 27, 2022 17:34
-
-
Save mkeneqa/540a4e97b2fc4d5194509d89bb99489e to your computer and use it in GitHub Desktop.
PHP Dates Helper Class
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 | |
namespace App\Onstream; | |
class DateHelper | |
{ | |
public static function GetDaysRemaining($date_from,$date_to): float | |
{ | |
$dt_to_future = strtotime($date_to); | |
$dt_from_src = strtotime($date_from); | |
$time_remain = $dt_to_future - $dt_from_src; | |
$days_remain = round((($time_remain/24)/60)/60); | |
return $days_remain; | |
} | |
public static function GetQuarterStartEndDates($the_date,$year,$dt_fmt = 'F'): array | |
{ | |
$quarters = array( | |
"q1"=>array("01-01","03-31"), | |
"q2"=>array("04-01","06-30"), | |
"q3"=>array("07-01","09-30"), | |
"q4" => array("10-01","12-31") | |
); | |
$dates = []; | |
foreach ($quarters as $key=>$dt){ | |
$start_date = $year.'-'.$dt[0]; | |
$end_date = $year.'-'.$dt[1]; | |
if(self::DateWithinRange($start_date,$end_date,$the_date)){ | |
//return full text representation of the month eg. April | |
$dates = array( | |
'start_month'=>date($dt_fmt, strtotime($start_date)), | |
'end_month'=>date($dt_fmt, strtotime($end_date)), | |
'quarter'=>$key | |
); | |
break; | |
} | |
} | |
return $dates; | |
} | |
public static function DateWithinRangeByNumberOfDays($date_to_start_from, $date_range_start, $date_range_end, $days): bool | |
{ | |
$prefix = ($days < 0 ) ? '-' : '+'; | |
$date = strtotime($prefix.$days." day", $date_to_start_from); | |
$selected_date = date('Y-m-d', $date); | |
return self::DateWithinRange($date_range_start,$date_range_end, $selected_date); | |
} | |
public static function DateWithinRange($start_date, $end_date, $selected_date): bool | |
{ | |
$start_ts = strtotime($start_date); | |
$end_ts = strtotime($end_date); | |
$selected_ts = strtotime($selected_date); | |
return (($selected_ts >= $start_ts) && ($selected_ts <= $end_ts)); | |
} | |
public static function GetTimespanDateSet($selected_month, $selected_year): array | |
{ | |
//['start_date'], ['end_date'], ['start_date_six_months_ago']; | |
$numMonths = 0; | |
$dt['start_date'] = self::GetFirstDayOfXMonths($selected_month, $numMonths, $selected_year); | |
$dt['end_date'] = self::GetLastDayOfXMonths($selected_month, $numMonths, $selected_year); | |
$dt['start_date_six_months_ago'] = self::GetFirstDayOfXMonths($selected_month, -6, $selected_year); | |
return $dt; | |
} | |
public static function GetFirstDayOfXMonths(string $selected_month, $offset = 0, $year = 'this year'):string | |
{ | |
$offset = ($offset >= 0) ? '+'.$offset : $offset; | |
return date('Y-m-d', strtotime('first day of '.$selected_month.' '.$year.$offset.' months')); | |
} | |
public static function GetLastDayOfXMonths(string $selected_month, $offset = 0, $year = 'this year'):string | |
{ | |
//past or future months | |
$offset = ($offset >= 0) ? '+'.$offset : $offset; | |
return date('Y-m-d', strtotime('last day of '.$selected_month.' '.$year.$offset.' months')); | |
} | |
public static function FirstDayOfYear($year_offset=0, $fmt='Y-m-d') | |
{ | |
$yr = date('Y') + $year_offset; | |
return date($fmt, strtotime('first day of january ' . $yr)); | |
} | |
public static function LastDayOfYear($year_offset=0, $fmt='Y-m-d') | |
{ | |
$yr = date('Y') + $year_offset; | |
return date($fmt, strtotime('last day of december ' . $yr)); | |
} | |
public static function TodaysDate($fmt='Y-m-d', $year_offset=0, $month_offset=0, $day_offset=0) | |
{ | |
return date($fmt, strtotime('now '.$year_offset.' year ' . $month_offset . ' month ' . $day_offset . ' day')); | |
} | |
public static $MonthsFullString = array( | |
0=>'december', | |
1=>'january', | |
2=>'february', | |
3=>'march', | |
4=>'april', | |
5=>'may', | |
6=>'june', | |
7=>'july', | |
8=>'august', | |
9=>'september', | |
10=>'october', | |
11=>'november', | |
12=>'december' | |
); | |
public static function GetAdvancedDateSet($month, $year): array | |
{ | |
$dates_set['curr_month']['start_date'] = DateHelper::GetFirstDayOfXMonths($month, 0, $year); | |
$dates_set['curr_month']['end_date'] = DateHelper::GetLastDayOfXMonths($month, 0, $year); | |
$dates_set['curr_month']['start_date_six_months_ago'] = DateHelper::GetFirstDayOfXMonths($month, -6, $year); | |
$dates_set['prev_month']['start_date'] = DateHelper::GetFirstDayOfXMonths($month, -1, $year); | |
$dates_set['prev_month']['end_date'] = DateHelper::GetLastDayOfXMonths($month, -1, $year); | |
$dates_set['prev_month']['start_date_six_months_ago'] = DateHelper::GetFirstDayOfXMonths($month, -6, $year); | |
$dates_set['last_year']['start_date'] = DateHelper::GetFirstDayOfXMonths($month, 0, $year - 1); | |
$dates_set['last_year']['end_date'] = DateHelper::GetLastDayOfXMonths($month, 0, $year - 1); | |
$dates_set['last_year']['start_date_six_months_ago'] = DateHelper::GetFirstDayOfXMonths($month, -6, $year - 1); | |
return $dates_set; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment