How to determine the day of the week, given the month, day and year in PHP
I was having a problem determining the day of the week for birth dates before 1970 in PHP (mktime, strtotime, etc), so I wrote my own function to handle any date.
For more details about the math, read this: How to determine the day of the week
Here is some PHP code:
/*
note: The Gregorian calendar, which is our civil calendar, was introduced
in 1582 (1752 in English-speaking countries, and only in 1919 in Russia).
So one has to know whether the Gregorian or the old Julian calendar is being used.
Apparently Uspensky and Heaslet gave the following formula to compute the
day of week for any date, in Elementary Number Theory, 1939, for the Gregorian calendar:
W = (d + floor(2.6m - 0.2) - 2C + Y + floor(Y/4) + floor(C/4)) mod 7
where floor() denotes the integer floor function,
d is day (1 to 31)
m is month (1 = March, …, 10 = December, 11 = Jan, 12 = Feb),
treat Jan & Feb as months of the preceding year (subtract 1 from year)
C is century (1987 has C = 19)
Y is year (1987 has Y = 87, except Y = 86 for Jan & Feb)
W is week day (0 = Sunday, …, 6 = Saturday)
*/
function day_of_week($mm, $dd, $yyyy)
{
$m = (int)$mm;
$d = (int)$dd;
$y = (int)$yyyy;
/* adjust month: i.e. March=1, …, December=10, January=11, February=12 */
if ($m < 3) {
$m = $m + 10;
$y = $y - 1;
}
else
$m = $m - 2;
$c = (int)($y/100); // get century
$yy = (int)( $y - ($c*100) ); // remove century from year
/* weekday formula:
W = (d + floor(2.6m - 0.2) - 2C + Y + floor(Y/4) + floor(C/4)) mod 7
where floor() denotes the integer floor function,
d is day (1 to 31)
m is month (1 = March, …, 10 = December, 11 = Jan, 12 = Feb),
treat Jan & Feb as months of the preceding year (subtract 1 from year)
C is century (1987 has C = 19)
Y is year (1987 has Y = 87, except Y = 86 for Jan & Feb)
W is week day (0 = Sunday, …, 6 = Saturday)
*/
$f1 = floor( (2.6 * $m) - 0.2 );
$f2 = floor( $yy / 4 );
$f3 = floor( $c / 4 );
$wday = ( $d + $f1 + $yy + $f2 + $f3 - (2 * $c) ) % 7;
if ($wday == 0) $res = “Sunday”;
elseif ($wday == 1) $res = “Monday”;
elseif ($wday == 2) $res = “Tuesday”;
elseif ($wday == 3) $res = “Wednesday”;
elseif ($wday == 4) $res = “Thursday”;
elseif ($wday == 5) $res = “Friday”;
else $res = “Saturday”;
return($res);
}
Use something like this:
enjoy!
life purpose calculator
I used PHP to create a Life-Purpose calculator as described in Dan Millman’s book The Life You Were Born to Live. I was inspired, and maybe others will be too.
Enjoy!
part 2: how i keep a rails app alive on DreamHost
Update: After a lot of monitoring and testing, none of the following is necessary to keep a Rails app alive on DreamHost. It seems that just using curl to hit a specific page (controller+view) every 5 minutes is enough to keep a Rails app alive. I noticed while monitoring that DreamHost seems to kill/restart dispatch.fcgi every 6 hours: around midnight, 6am, noon, and 6pm.
Here is part 1.
I use fcgimaint.rb to periodically kill all dispatch.fcgi’s (you know the one in your rails app public folder).
So in web panel, click Goodies, click Cron Jobs, click Add New Cron Job.
Then in the Command to run: box enter something like the following:
/home/you/somedir/fcgimaint.rb
in other words, use the directory where you put the fcgimaint.rb file that you downloaded from the link above.
Also, in the When to run: box, select Custom, then in Minutes select Every 30 minutes.
Note: there is probably a way to do this using crontab, but the DreamHost web panel is straightforward to use and if you check Use locking:, this prevents the job from running more than once at a time (a good thing).
I am beginning to think that this is not necessary, because maybe the curl cron job solves the problem.
part 1: how i keep a rails app alive on DreamHost
I use curl to hit my website and keep the DreamHost FastCGI eater from snacking on dispatch.fcgi (you know the one in your rails app public folder).
So in web panel, click Goodies, click Cron Jobs, click Add New Cron Job.
Then in the Command to run: box enter something like the following:
curl –silent http://your.app.com/ > /dev/null
Also, in the When to run: box, select Selected Minutes, then select 3 or more minutes of the hour to execute the cron job (I selected: 4, 34, and 57).
Note: there is probably a way to do this using crontab, but the DreamHost web panel is straightforward to use and if you check Use locking:, this prevents the job from running more than once at a time (a good thing).
About
As a freelance web developer, most of the posts will be about web development, and most of those will be about learning and using Ruby, web application frameworks (such Rails, Merb, and so on). I have used other languages and other frameworks, but I have enjoyed using Ruby on Rails (RoR) the most.
A lifetime ago, it seems, I spent my days developing 3270 terminal-based applications on a mainframe using COBOL and Natural/AdaBas. Another lifetime ago, I built a Super ELF computer that was in a wooden box (does that make it green? probably, not … sorry trees) and programs were entered using a hex keypad. I think the Super ELF’s microprocessor is on Voyager I and going where no microprocessor has gone before (not wanting to be abducted … no offense to any aliens who power their travels based on a cpu in a wooden box).
Check this out: once I worked on a project whose goal was to allow web access to AdaBas databases. Sounds simple, but there was (at the time) no simple API from an application running on a web server to communicate with AdaBas. So our solution was to set up a cluster of PC servers with Apache + Tomcat + Java servlets + Struts + a custom Java API that communicated with CICS (via a non-standard use of TCP/IP). That was tier 1. Tier 2, was on a IBM mainframe and consisted of CICS + a custom COBOL API that communicated with the custom Java API on tier 1 + a custom Natural API that communicated with the COBOL API . Tier 3, not an actual tier (but a logical one), was comprised of Natural programs which used the custom API and retrieved sets of records from AdaBas based on the user requested parameters received from the API. All of this, to satisfy about 8,000 users. Why not just move the data to Oracle or another database? You guessed it, the project was for a federal government agency (taxes well spent, huh?). Again, you guessed it, that is why I am now a freelance web developer.
I digress … oh wait, that’s the point of a blog.
After all of that, RoR is like a dream, well, most of the time (ok, not really). Hosting RoR is a pain (well, on a shared DreamHost account), but I am sure that was also true when PHP and Java were nubies.