Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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' );
- }
Add Comment
Please, Sign In to add comment