Requests | Blesta

Requests

Add Helpful Functions to Date Helper

T. Chance Vecchitto shared this idea 4 months ago
Under Consideration

We have a use-case where we need to know how many days a certain month has, how many weeks a certain month has, and what week number a specific date falls into. This information is used in creating reports where data is displayed in various timelines for comparison. All of this can be retrieved using php's date(), mktime(), and strtotime() functions but it is easier to have wrapper methods for greater re-usability. I believe these added functions wouldbe good to have in the official release of the Blesta Date helper class.

/**

* Return the total number of days in a given month.

* @param int $month

* @param int $year

* @return int

*/

public function daysInMonth($month, $year)

{

return date( "t", mktime( 0, 0, 0, $month, 1, $year ) );

}

/**

* Return the total number of weeks of a given month.

* @param int $month

* @param int $year

* @return int

*/

public function weeksInMonth($month, $year)

{

$first_day_of_week = date( "w", mktime( 0, 0, 0, $month, 1, $year ) );

$days_in_month = date( "t", mktime( 0, 0, 0, $month, 1, $year ) );

$day = 1;

$count_weeks = 0;

while( $day <= ( $days_in_month + $first_day_of_week ) )

{

$day += 7;

$count_weeks++;

}

return $count_weeks;

}

/**

* Return week number that a specific date falls into

* @param string $date

* @param int $start_day_of_week (0=Sunday ... 6=Saturday)

* @return int

*/

public function weekOfMonth($date, $start_day_of_week)

{

// extract date parts

list( $year, $month, $day ) = explode( '-', date( 'Y-m-d', strtotime( $date ) ) );

// current week//, min 1

$week = 1;

// for each day since the start of the month

for( $i = 1; $i <= $day; ++$i )

{

// if loop day is start_day_of_week and is not the first day of month

if( $i > 1 && date( 'w', strtotime( "$year-$month-$i" ) ) == $start_day_of_week )

{

++$week;

}

}

// ensure week determined is not greater than total weeks

$weeks = $this->weeksInMonth( $month, $year, $start_day_of_week );

if( $week > $weeks ) { $week = $weeks; }

return $week;

}

Comments (1)

photo
1

I overlooked an additional method that would be nice to have added to the Date helper class. This method is used by us in reports where we show totals of each row in several columns....each column represents a specific week in the month being reported on.

An example is the month of July 2024. This function returns a 5-element array where each element contains the date of the first and last day of each week:

Array

(

[0] => Array

(

[0] => 2024-07-01T00:00:00+00:00

[1] => 2024-07-06T00:00:00+00:00

)

[1] => Array

(

[0] => 2024-07-07T00:00:00+00:00

[1] => 2024-07-13T00:00:00+00:00

)

[2] => Array

(

[0] => 2024-07-14T00:00:00+00:00

[1] => 2024-07-20T00:00:00+00:00

)

[3] => Array

(

[0] => 2024-07-21T00:00:00+00:00

[1] => 2024-07-27T00:00:00+00:00

)

[4] => Array

(

[0] => 2024-07-28T00:00:00+00:00

[1] => 2024-07-31T00:00:00+00:00

)

)

/**

* Return array of weekly date ranges for each week of a specific month/year

* @param int $month

* @param int $year

* @return array

*/

public function weeklyDateRanges($month, $year)

{

$date = new DateTime( "now" );

$date->setDate( $year, $month, 1 );

$date->setTime( 0, 0, 0 );

//last day of the month

$maxDay = intval( $date->format("t") );

//getting the first sunday

$dayOfTheWeek = $date->format( "N" );

if($dayOfTheWeek != 0)

{

$diff = 7 - $dayOfTheWeek;

if($dayOfTheWeek <= 6)

{

$from = $date->format( "c" );

$diff2 = 6 - $dayOfTheWeek;

$date->modify( sprintf( "+%d days", $diff2 ) );

$to = $date->format( "c" );

$dates[] = array( $from, $to );

$diff -= $diff2;

}

$date->modify( sprintf( "+%d days", $diff ) );

}

// iterate while we are in the current month

while( intval( $date->format( "n" ) ) == $month )

{

$from = $date->format( "c" );

$date->modify( "+6 days" );

if( intval( $date->format( "n" ) ) > $month )

{

$date->setDate( $year, $month, $maxDay );

}

$to = $date->format( "c" );

$date->modify( "+1 days" );

$dates[] = array( $from, $to );

}

return $dates;

}