date question

got a quick question about finding specific days. I have a script that I need to run on the second Friday of each month and since I can’t do it without editing a file on the server (don’t have access to the file), I have to find a way to do it through php. The best i can do is have it run every Friday. But that’ll lead another issue once the import script kicks in.

This is what I have so far[php]$fom = mktime(0, 0, 0, $month, 1, $year);
$secfri = strtotime(date(‘m/d/Y’, $fom), ‘+7 days’);
$curdate = strtotime(date(‘m/d/Y’));

if($secfri == $curdate) {
for($i=0; $i < count($command); $i++) {
exec($command[$i], $out, $err);
}

if(isset($out)) {
	foreach ($out as $line) {
		echo "$line\r\n";
	}
} else {
	foreach ($err as $line) {
		echo "$code: $line\r\n";
	}
}

}[/php]What i’m not sure how php calculates an offset, whether it counts the current date or if it moves to the next day. each Friday is 7 days from the previous Friday, so i’m thinking what I have should work.

Don’t you have access to set up cron jobs?

I do, but from everything ive read, its not possible to set it up through cpanel. you have to edit some file that I dont seem to have access to. i run this file through a cron job, but I have to set it up to run every friday. but, I dont need it downloading files every week, just the 2nd friday.

I think your script is assuming $fom is the first Friday…

[php]$fom = mktime(0, 0, 0, $month, 1, $year);[/php]

Which it’s not, it’s just the first day of the current month.

You’ll have to do something like this…

[php]switch (date(‘D’, $fom)
{
case “Sat”
$secfri = strtotime(date(‘m/d/Y’, $fom), ‘+13 days’);
break;
case “Sun”
$secfri = strtotime(date(‘m/d/Y’, $fom), ‘+12 days’);
break;
case “Mon”
$secfri = strtotime(date(‘m/d/Y’, $fom), ‘+11 days’);
break;
case “Tue”
$secfri = strtotime(date(‘m/d/Y’, $fom), ‘+10 days’);
break;
case “Wed”
$secfri = strtotime(date(‘m/d/Y’, $fom), ‘+9 days’);
break;
case “Thu”
$secfri = strtotime(date(‘m/d/Y’, $fom), ‘+8 days’);
break;
case “Fri”
$secfri = strtotime(date(‘m/d/Y’, $fom), ‘+7 days’);
break;
}[/php]

My Math may be a little off…

it $fom is the first of the month, but under that, I use strtotime to add the wrong amount of days.

If you use the switch statement that I shown, wouldn’t that add the correct # of days?

I see what the switch is for. Might not need to use that switch though. I did some reading on strtotime and it appears that after php 5.1, they fixed how it gets the days, so if I do strtotime($fom, ‘second friday’), I should get the second Friday.

You might need to do…

strtotime($fom, ‘second friday of this month’)

can’t hurt to try it. I’m already getting the current month for another part of the script, do I still need to put it in the strtotime function? Below is the entire script
[php]<?php
$cc = array(‘AE’,‘AU’,‘CA’,‘CH’,‘DK’,‘EU’,‘GB’,‘HK’,‘ID’,‘IL’,‘IN’,‘JP’,‘MX’,‘NO’,‘NZ’,‘RU’,‘SA’,‘SE’,‘SG’,‘TR’,‘TW’,‘US’,‘WW’,‘ZA’);
$month = date(‘m’);
$year = date(‘Y’);

switch($month) {
case ‘10’: // October
$cperiod = ‘12’;
break;
case ‘11’: // November
$cperiod = ‘01’;
break;
case ‘12’: // December
$cperiod = ‘03’;
break;
case ‘01’: // January
$cperiod = ‘03’;
break;
case ‘02’: // February
$cperiod = ‘04’;
break;
case ‘03’: // March
$cperiod = ‘05’;
break;
case ‘04’: // April
$cperiod = ‘06’;
break;
case ‘05’: // May
$cperiod = ‘07’;
break;
case ‘06’: // June
$cperiod = ‘08’;
break;
case ‘07’: // July
$cperiod = ‘09’;
break;
case ‘08’: // August
$cperiod = ‘10’;
break;
case ‘09’: // September
$cperiod = ‘11’;
break;
}

$command = array();
foreach($cc as $code) {
$command[] = 'cd /home/venzo/public_html/admin/files/ && java Autoingestion music.properties xxxxxxxxxxxx ‘.$code.’ DRR ‘.$year.’ '.$cperiod;
}

$fom = mktime(13, 0, 0, $month, 1, $year);
$secfri = strtotime(‘second friday’, $fom);
$curdate = time();

if($secfri === $curdate) {
for($i=0; $i < count($command); $i++) {
exec($command[$i], $out, $err);
}

if(isset($out)) {
	foreach ($out as $line) {
		echo "$line\r\n";
	}
} else {
	foreach ($err as $line) {
		echo "$code: $line\r\n";
	}
}

}
?>[/php]

Can’t you do this and forget all that mumbo jumbo?

$secfri = strtotime(‘second friday of this month’)

The switch isn’t related to finding the second Friday, that’s for telling iTunes what report to download and for which period it should be for. I posted the entire script so you could get an idea of what I was using it for and maybe help out someone else.

On top of all that mumbo jumbo, i’m already finding the current month, won’t php figure out from that what month I want the Friday in?

I was looking at this on my iPad and noticed something, had to get out of bed to type this… I think you’ll like it. I got rid of all the mumbo jumbo…

[php]<?php
$cc = array(‘AE’,‘AU’,‘CA’,‘CH’,‘DK’,‘EU’,‘GB’,‘HK’,‘ID’,‘IL’,‘IN’,‘JP’,‘MX’,‘NO’,‘NZ’,‘RU’,‘SA’,‘SE’,‘SG’,‘TR’,‘TW’,‘US’,‘WW’,‘ZA’);

$year = date(‘Y’);
$cperiod = date(“m”,strtotime("+2 month"));

$command = array();
foreach($cc as $code) {
$command[] = 'cd /home/venzo/public_html/admin/files/ && java Autoingestion music.properties xxxxxxxxxxxx ‘.$code.’ DRR ‘.$year.’ '.$cperiod;
}

$secfri = strtotime(“second friday of this month”);
$curdate = time();

if($secfri === $curdate) {
for($i=0; $i < count($command); $i++) {
exec($command[$i], $out, $err);
}

if(isset($out)) {
	foreach ($out as $line) {
		echo "$line\r\n";
	}
} else {
	foreach ($err as $line) {
		echo "$code: $line\r\n";
	}
}

}
?>[/php]

My only worry is that cperiod, I had a lot of errors when I was setting that up because apple is extremely picky about that. I hadn’t noticed it earlier, but it is actually 2 months ahead. I’ll give it a try and see what happens.

edit:
I did some quick testing using echoes and it seems to put out the right command to download the files (1 per country). I echoed out $secfri and it returned the right date. 2 more real world tests, 1 comes this Friday when it runs from the cronjob and the second is on Nov. 8th. If the evaluation is right, it should download it.

Yeah, I tested the dates before I posted it to make sure it was accurate. I learned a lot from trying to help.

Having to create that script was a challenge. I’m sick of apple right now.

Well, apparently this was all for nothing, at least for me. Hopefully someone learns something from it! Now my boss wants the reports downloaded on the 1st and 20th of each month, which I can easily set up with a standard cronjob.

It’s good to be the boss…

Sponsor our Newsletter | Privacy Policy | Terms of Service