Created
April 6, 2011 15:15
-
-
Save nabeelio/905831 to your computer and use it in GitHub Desktop.
Partial class, some useful date/time functions
This file contains 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
class DateFunctions | |
{ | |
/** | |
* Get the months in an array since a start date | |
* | |
* @param mixed $start The date to get since | |
* @return array Returns the list of months | |
*/ | |
public static function getMonthsSinceDate($start) { | |
$months = array(); | |
if (!is_numeric($start)) { | |
$start = strtotime($start); | |
} | |
$end = date('Ym'); | |
do { | |
# Get the months | |
$month = date('M Y', $start); | |
$months[$month] = $start; # Set the timestamp | |
$start = strtotime('+1 month +1 day', strtotime($month)); | |
# Convert to YYYYMM to compare | |
$check = intval(date('Ym', $start)); | |
} while ($check <= $end); | |
return $months; | |
} | |
/** | |
* Get the months in an array since a start date | |
* | |
* @param mixed $start The date to get start | |
* @param mixed $end The date to get ending | |
* @return array Returns the list of months | |
*/ | |
public static function getMonthsInRange($start, $end) { | |
$months = array() | |
if (!is_numeric($start)) { | |
$start = strtotime($start); | |
} | |
if (!is_numeric($end)) { | |
$end = strtotime($end); | |
} | |
$end = intval(date('Ym', $end)); | |
/* Loop through, adding one month to $start each time | |
*/ | |
do { | |
$month = date('M Y', $start); # Get the month | |
$months[$month] = $start; # Set the timestamp | |
$start = strtotime('+1 month +1 day', strtotime($month)); | |
//$start += (SECONDS_PER_DAY * 25); # Move it up a month | |
$check = intval(date('Ym', $start)); | |
} while ($check <= $end); | |
return $months; | |
} | |
/** | |
* Add two time's together (1:30 + 1:30 = 3 hours, not 2.6) | |
* Or 1.80 + 1:80, mixed times. Any decimal over .60 is assumed a fraction | |
* | |
* @param mixed $time1 Time one | |
* @param mixed $time2 Time two | |
* @return mixed Total time | |
* | |
*/ | |
public static function AddTime($time1, $time2) | |
{ | |
$time1 = str_replace(':', '.', $time1); | |
$time2 = str_replace(':', '.', $time2); | |
$time1 = number_format((double)$time1, 2); | |
$time2 = number_format((double)$time2, 2); | |
$time1 = str_replace(',', '', $time1); | |
$time2 = str_replace(',', '', $time2); | |
$t1_ex = explode('.', $time1); | |
$t2_ex = explode('.', $time2); | |
# Check if the minutes are fractions | |
# If they are (minutes > 60), convert to minutes | |
if($t1_ex[1] > 60) { | |
$t1_ex[1] = intval((intval($t1_ex[1])*60)/100); | |
} | |
if($t2_ex[1] > 60) { | |
$t2_ex[1] = intval((intval($t2_ex[1])*60)/100); | |
} | |
$hours = ($t1_ex[0] + $t2_ex[0]); | |
$mins = ($t1_ex[1] + $t2_ex[1]); | |
while($mins >= 60) { | |
$hours++; | |
$mins -= 60; | |
} | |
# Add the 0 padding | |
if(intval($mins) < 10) | |
$mins = '0'.$mins; | |
$time = number_format($hours.'.'.$mins, 2); | |
$time = str_replace(',', '', $time); | |
return $time; | |
#return $hours.'.'.$mins; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment