...

# calculate 1st Tuesday ea. January?

fuzzy1
11-14-2006, 04:36 PM
Hey All,
I need a little help. Total noob so please be patient.

The idea is to establish thirteen 28 day contact_periods or contact_cycles for a CRM application, on a 4 day offset, for two groups of contacts.

Period_1 begins on the first Tuesday of each January for Group_A and on the first Saturday of each January for Group_B -- from now to eternity.

Period_2 begins 28 days after Period_1 ... something like... \$timedate = new TimeDate();
\$today = \$timedate->handle_offset(date("Y-m-d"), \$timedate >dbDayFormat, false);

\$StartCycleYear = (1stTuesday_ea_January); //if Group_A || if Group_B ????

\$StartPeriod_2 = \$StartCycleYear("Y-m-d", strtotime('+28 days'));
\$StartPeriod_3 = \$StartPeriod_2 ("Y-m-d", strtotime('+28 days'));
//etc...

if (\$today < \$StartPeriod_2) {
\$Period='\$StartCycleYear';
} else if (\$today > \$StartPeriod_2 && \$today < \$StartPeriod_3 ){
\$Period= '\$StartPeriod_2';
}
//update visit_by date
\$update_visit_by =" UPDATE accounts,contacts,emailman SET accounts.visit_by = '\$Period' WHERE accounts.name = contacts.agency_name AND emailman.related_id = contacts.id AND emailman.marketing_id = '8c63da95-3b5f-3348-3465-45245671f1e1' ";
\$result = @mysql_query (\$update_visit_by); // Run the query.

I offer a rough sketch only. I know the syntax is off, and I can see that such an approach is going to grow into an
unwieldy mass of some 100 odd lines, but if it makes any sense at all, how might I establish/calculate \$StartCyleYear
and then add to that date in 28 day increments with conditions for Group_A || Group_B?
Otherwise, I'm MORE THAN OPEN to ANY suggestions as to how I might rethink my approach.

firepages
11-14-2006, 05:02 PM
this is why I luuurrrve PHP...

<?php
\$start = strtotime("first tuesday jan 2007");
echo date('Y-m-d',\$start)."\n";
\$te = (28 * (3600*24));
while(\$x<=12){
\$f += \$te;
++\$x;
echo date('Y-m-d',(\$start+\$f))."\n";
}
?>

how you use those dates in your scenario I dont know, at worst you can just load them into an array and use later (just change the start date for the second group.

fuzzy1
11-14-2006, 05:14 PM
Cool! I was sure I had way too much code in the works.
Not sure I understand your example, but it looks promising.
No doubt I'll have follow up questions, but for now...
I'll just have to play with it and see what I can work out.
Till then...
Many THANKS!

fuzzy1
11-14-2006, 06:13 PM
Well...
I twisted and turned this Seven ways from Sunday, but ...<?php
\$start = strtotime("first tuesday jan 2007");
echo date('Y-m-d',\$start)."\n";
?> ... returns 2007-01-14 , but the first tuedsday in January 2007 is 2007-01-02 so... hmmmm?

What am I missing?

firepages
11-14-2006, 06:42 PM
weird , my PHP (5.1.6) returns ...

2007-01-02
2007-01-30
2007-02-27
2007-03-27
2007-04-24
2007-05-22
2007-06-19
2007-07-17
2007-08-14
2007-09-11
2007-10-09
2007-11-06
2007-12-04
2008-01-01

which looks right ... I see there have been mods to strtotime() so perhaps thats the issue if you are on PHP < 5.1 ?? ...will have a looksee

fuzzy1
11-14-2006, 07:10 PM
Aha!!!

I'm running on 4.4.2, cause 5.1.2 is not supported in SugarCRM 4.0.1f (though I've yet to experience any failings in the CRM running 5.1.2)

If there is a workaround for php4.4.2 I'd sure like to learn about it.
Meanwhile, switching to 5.1.2 it works as promised.
Thanks Again!
TJ

fuzzy1
11-14-2006, 08:29 PM
So, moving right along...

Now I'm thinking ...
<?php
\$start = strtotime("first tuesday jan 2007");

\$te = (28 * (3600*24));
\$f += \$te;
\$period_1= date('Y-m-d',\$start)."\n";
\$period_2 = date('Y-m-d',(\$period_1+\$f))."\n";
\$period_3 = date('Y-m-d',(\$period_2+\$f))."\n";
echo \$period_1."<br>";
echo \$period_2."<br>";
echo \$period_3."<br>";
?>
... but that only gets me
2007-01-02
1970-01-28
1970-01-28

I guess it's the "add to an array" part that I need help with now.:p

fuzzy1
11-14-2006, 10:38 PM
Ding !)))
In Round 3 I tried...<?php
\$start = strtotime("first tuesday jan 2007");
echo date('Y-m-d',\$start)."\n";
\$te = (28 * (3600*24));
while(\$x<=12){
\$f += \$te;
++\$x;
\$cycleYear = date('Y-m-d',(\$start+\$f));
\$array = explode (' ', \$cycleYear);
}

echo '<select name="endDate">';
foreach (\$array as \$key => \$value) {
echo "<option value=\"\$key\">
\$value</option>\n";
}
echo '</select>';
?> which get's me a dropdown with a single option 2008-01-01 (should be 2007-01-28) How would I convert \$cycleYear to an array?

fuzzy1
11-15-2006, 12:15 AM
<?php
\$array = array();
\$start = strtotime("first tuesday jan 2007");
echo date('Y-m-d',\$start)."\n";
\$te = (28 * (3600*24));
while(\$x<=12){
\$f += \$te;
++\$x;
// echo date('Y-m-d',(\$start+\$f))."\n";
//}
\$array[] = date('Y-m-d',(\$start+\$f));
}

echo '<select name="endDate">';
foreach (\$array as \$key => \$value) {
echo "<option value=\"\$key\">
\$value</option>\n";
}
echo '</select>';
?> :thumbsup: