Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4082332
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:10:28+00:00 2026-05-20T18:10:28+00:00

I’ve looked at at least 2 dozen topics about this and haven’t really found

  • 0

I’ve looked at at least 2 dozen topics about this and haven’t really found a good answer yet, so I come to you to ask once again for answers regarding the dreaded topic of Repeating Events.

I’ve got Daily, Weekly, Monthly, and Yearly repeats working out fine for now (I still need to revamp the system with Exception events and whatnot, but it works for the time being). But, we want to be able to add the ability to repeat events on the (1st,2nd,3rd,4th,5th) [Sun|Mon|Tue|Wed|Thu|Fri|Sat] of every month, every other month, and every three months.

Now, if I can just understand the logic for the every month, I can figure out the every other month and the every three months.

Here’s a bit of what I have so far (note: I’m not saying I have the best way of doing it or anything, but the system is one we update very slowly over time when we aren’t busy with other projects, so I make the code more efficient as I have the time).

First I get the starting and ending dates formatted for date calculations:

$ending = $_POST['end_month'] . "/" . $_POST['end_day'] . "/" . substr($_POST['end_year'], 2, 2);
$starting = $_POST['month'] . "/" . $_POST['day'] . "/" . substr($_POST['year'], 2, 2);

Then I get the difference between those two to know how many times to repeat using a function I’m fairly certain I found on here some time ago and dividing that amount by 28 days to get just about how many TIMES it needs to repeat so that there is one a month:

$repeat_number = date_diff($starting, $ending) / 28;
//find the difference in DAYS between the two dates
function date_diff($old_date, $new_date) {
$offset = strtotime($new_date) - strtotime($old_date);
return $offset/60/60/24;
}

Then I add the (1st,2nd,etc…) part to the [Sun|Mon|etc…] part to figure out what they are looking for giving me somehthing like ‘first Sunday’ :

$find = $_POST['custom_number']. ' ' . $_POST['custom_day'];

Then I use a loop that runs the number of times this needs to repeat (the $repeat_number from above):

for($m = 0; $m <= $repeat_number; $m++) {
if($m == 0) {
     $month = date('F', substr($starting,0,2));
} else {
     $month = date('F', strtotime($month . ' + ' . $m . ' months'));
}
$repeat_date = strtotime($find . ' in ' . $month);
}

Now, I’m aware this code doesn’t work, I at one time did have code that turned up the correct month and year for the repeat, but wouldn’t necessarily find the first tuesday or whatever it was that was being looked for.

If anyone could point me back in the right direction, it would be most appreciated. I’ve been lurking in the community for some time now, just started trying to actively participate recently.

Thanks in advance for any input or advice you can provide.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:10:29+00:00Added an answer on May 20, 2026 at 6:10 pm

    Here’s a possible alternative for you. strtotime is powerful, and the syntax includes really useful bits like comprehensive relative time and date wording.

    You can use it to generate the first through Nth specific weekdays of a specific month by using the format " of ". Here’s an example using date_create to invoke a DateTime object, but regular old strtotime works the same way:

    php > $d = date_create('Last Friday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
    Friday March 25 2011 00:00:00
    php > $d = date_create('First Friday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
    Friday March 04 2011 00:00:00
    php > $d = date_create('First Sunday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
    Sunday March 06 2011 00:00:00
    php > $d = date_create('Fourth Sunday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
    Sunday March 27 2011 00:00:00
    php > $d = date_create('Last Sunday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
    Sunday March 27 2011 00:00:00
    

    It will also overflow the month, if you ask for an invalid one:

    php > $d = date_create('Ninth Sunday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
    Sunday May 01 2011 00:00:00
    

    Note that it only works with the ordinal number wording. You can’t pass "1st" or "3rd", unfortunately.

    Once you use this to grab the proper Nth weekday, you can then simply add the number of needed weekdays to skip (7 for one week, 14 for two, 21 for three, etc) as required until the designated end date or designated number of weeks has passed. Once again, strtotime to the rescue, using the second argument to chain together relativeness:

    php > echo date('l F d Y H:i:s', strtotime('+14 days', strtotime('First Thursday of March 2011')));
    Thursday March 17 2011 00:00:00
    

    (My local copy also accepted '+14 days First Thursday of March 2011', but that felt kind of weird.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.