Tuesday, October 30, 2007

PHP Date Math with date() and mktime()

Normal date math in PHP does not account for leap years (or apparently Daylight Savings Time for that matter).

If the current month is January, 2008 and you run
$newMonthText = date("F", mktime(0, 0, 0, date("m")+1, date("d"), date("Y")));
you'll get "March" for the value instead of what you probably want, which is "February." The good people on php.net have lots to say about this. (I think the other condition is that "skipping" Feburary happens only on the 30th or 31st of the current month.)

The way to deal with this is to use
$newMonthText = date("F", mktime(0, 0, 0, date("m")+1, 1, date("Y")));
which will return "February" as intended.

Friday, October 19, 2007

List of APIs

Here's a list of APIs that seems to be updated regularly.

I always feel like I should be doing more to make mashups with these as I work on new products.

Saturday, October 13, 2007

Another Great List of Development Tools

Scott Hanselman has a great list of development tools on his blog. It's so easy to forget about some of these.

Wednesday, October 10, 2007

Web Development Toolbox

Mashable has a very handy list of web devleopment tools. Check it out. They have several other lists on different topics that are equally interesting, too.

Handle All Application Errors, Log Them in Event Viewer, and Show a Friendly Message

I'm working on a simple way in an ASP.NET 2.0 website to 1) catch all application errors that are otherwise uncaught, 2) log these errors in the Event Viewer, and 3) display a friendly error message to the user when these errors occur, while trying to keep this code all in as few places as possible. Here's what I'm doing now. I'll modify this post if I change.

In global.asax I added:
void Application_Error(object sender, EventArgs e)
// Code that runs when an unhandled error occurs
/* Error information will be written to the Event Viewer logs and not displayed
* to the user. The error page will present a friendlier message. */
string fullError = "An error occurred. The page was: " + Request.Url.PathAndQuery + ". " +
"The message was: " + Server.GetLastError().Message + ". " +
"The stack trace was: " + Server.GetLastError().StackTrace + ". ";

/* We don't clear the error here so we can display it on the error page
* when tracing is enabled. The error is cleared there. */

System.Diagnostics.EventLog eLog = new System.Diagnostics.EventLog("Application", ".");
// Application is the log to use, . is the local machine.
eLog.Source = "MyApp";
eLog.WriteEntry(fullError, System.Diagnostics.EventLogEntryType.Error);


In error.aspx I added:
using System.Diagnostics;

public partial class error : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
Trace.Write("ERROR: " + Server.GetLastError().Message);
/* Clear error here, not in Global.asax because
* we want to put it in the trace messages. */

In testerror.aspx, a page that intentionally throws an error to test the setup, I added:

protected void Page_Load(object sender, EventArgs e)
// This throws an exception so we can test logging to the event viewer.
throw (new ArgumentNullException());

Run a VS.NET 2005 Site as a "Root" Site

If you want to run a Visual Studio.NET 2005 website as a root site when debugging or previewing, take a look at Scott Gutherie's post on this. Instead of having your site run as http://localhost:1234/myapp it will run as http://localhost:1234/. Makes life easier when doing relative paths.

By the way, if you're a .NET developer, you should be reading his blog regularly.

Errors with Page Trace Enabled and ASP.NET AJAX

If you have page level tracing enabled on your site and you begin adding ASP.NET AJAX controls, you may see errors that say something along the lines of "Sys.WebForms.PageRequestManagerParseErrorException The message received from the server can not be parsed" when you preview or debug those pages. The ones I experienced appeared in a JavaScript alert popup.

If you have Trace="true" on the page in question, remove it. If you have your web.config file set for tracing, change it to: <trace enabled="true" localOnly="true" pageOutput="false" />

Browse or debug the page and then go to the root of your application's trace.axd file (http://yourapp/trace.axd) to get to the trace information.

See http://samples.gotdotnet.com/quickstart/aspplus/doc/tracelogapp.aspx for more.