Guest User

Untitled

a guest
Dec 11th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. // returns an array with the dates for a recurring monthly rule
  2.  
  3. $dates = tscpl_recurring_monthly( $start_date, $end_date, 'fourth', 'tuesday );
  4.  
  5. function tscpl_recurring_weekly( $start_date, $end_date = null, $dow ) {
  6.  
  7. $days = array(
  8. 'sunday' => 0,
  9. 'monday' => 1,
  10. 'tuesday' => 2,
  11. 'wednesday' => 3,
  12. 'thursday' => 4,
  13. 'friday' => 5,
  14. 'saturday' => 6
  15. );
  16.  
  17. $zone = date_default_timezone_get();
  18. date_default_timezone_set( 'America/Chicago' );
  19.  
  20. if ( gettype( $dow ) == 'integer' )
  21. $dow = $days[ $dow ];
  22.  
  23. if ( empty( $start_date ) )
  24. $start_date = date( 'Y-m-d' );
  25.  
  26. $start_date = date( 'Y-m-d', strtotime( $start_date ) );
  27. $start_dow = (int) date( 'w', strtotime( $start_date ) );
  28.  
  29. if ( empty( $end_date ) ) {
  30. $end_date = date( 'Y-m-d', strtotime( current_time() . ' +18 weeks' ) ); // well into the next print calendar cycle
  31. } else {
  32. $end_date = date( 'Y-m-d', strtotime( $end_date ) );
  33. }
  34.  
  35. if ( gettype( $dow ) == 'string' )
  36. $dow = $days[ strtolower( $dow ) ];
  37.  
  38. // does our start date equal the DOW?
  39. if ( $start_dow < $dow ) {
  40. $start_date = date( 'Y-m-d', strtotime( $start_date . ' +' . ($dow - $start_dow) . ' days' ) );
  41. } elseif ( $start_dow > $dow ) {
  42. $start_date = date( 'Y-m-d', strtotime( $start_date . ' +' . (7 - $start_dow) . ' days' ) );
  43. }
  44.  
  45. $i = 0;
  46. $dates = array();
  47. while ( $i < 12 ) {
  48. $dt = date( 'Y-m-d', strtotime( $start_date . ' +' . $i . ' weeks' ) );
  49. if ( $dt > $end_date )
  50. break;
  51. $dates[] = $dt;
  52. $i++;
  53. }
  54.  
  55. date_default_timezone_set( $zone );
  56.  
  57. return $dates;
  58.  
  59. }
  60.  
  61. function tscpl_date( $format = 'Y-m-d H:i:s', $date ) {
  62. if ( gettype( $date ) == 'string' )
  63. $date = strtotime( $date );
  64. return date( $format, $date );
  65. }
  66.  
  67. function tscpl_date_by_text( $date, $text ) {
  68. $dt = new \DateTime( $date );
  69. return $dt->modify( $text )->format( 'Y-m-d 00:00:00' );
  70. }
Add Comment
Please, Sign In to add comment