Created
December 11, 2018 14:21
-
-
Save dnavarrojr/dd12ce8f81ca214754356d2ba8fa9054 to your computer and use it in GitHub Desktop.
Generate recurring monthly dates
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
// returns an array with the dates for a recurring monthly rule | |
$dates = tscpl_recurring_monthly( $start_date, $end_date, 'fourth', 'tuesday ); | |
function tscpl_recurring_weekly( $start_date, $end_date = null, $dow ) { | |
$days = array( | |
'sunday' => 0, | |
'monday' => 1, | |
'tuesday' => 2, | |
'wednesday' => 3, | |
'thursday' => 4, | |
'friday' => 5, | |
'saturday' => 6 | |
); | |
$zone = date_default_timezone_get(); | |
date_default_timezone_set( 'America/Chicago' ); | |
if ( gettype( $dow ) == 'integer' ) | |
$dow = $days[ $dow ]; | |
if ( empty( $start_date ) ) | |
$start_date = date( 'Y-m-d' ); | |
$start_date = date( 'Y-m-d', strtotime( $start_date ) ); | |
$start_dow = (int) date( 'w', strtotime( $start_date ) ); | |
if ( empty( $end_date ) ) { | |
$end_date = date( 'Y-m-d', strtotime( current_time() . ' +18 weeks' ) ); // well into the next print calendar cycle | |
} else { | |
$end_date = date( 'Y-m-d', strtotime( $end_date ) ); | |
} | |
if ( gettype( $dow ) == 'string' ) | |
$dow = $days[ strtolower( $dow ) ]; | |
// does our start date equal the DOW? | |
if ( $start_dow < $dow ) { | |
$start_date = date( 'Y-m-d', strtotime( $start_date . ' +' . ($dow - $start_dow) . ' days' ) ); | |
} elseif ( $start_dow > $dow ) { | |
$start_date = date( 'Y-m-d', strtotime( $start_date . ' +' . (7 - $start_dow) . ' days' ) ); | |
} | |
$i = 0; | |
$dates = array(); | |
while ( $i < 12 ) { | |
$dt = date( 'Y-m-d', strtotime( $start_date . ' +' . $i . ' weeks' ) ); | |
if ( $dt > $end_date ) | |
break; | |
$dates[] = $dt; | |
$i++; | |
} | |
date_default_timezone_set( $zone ); | |
return $dates; | |
} | |
function tscpl_date( $format = 'Y-m-d H:i:s', $date ) { | |
if ( gettype( $date ) == 'string' ) | |
$date = strtotime( $date ); | |
return date( $format, $date ); | |
} | |
function tscpl_date_by_text( $date, $text ) { | |
$dt = new \DateTime( $date ); | |
return $dt->modify( $text )->format( 'Y-m-d 00:00:00' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment