It isn't obvious from the above, but you can insert a letter of the alphabet directly into the date string by escaping it with a backslash in the format string. Note that if you are using "double" speech marks around the format string, you will have to further escape each backslash with another backslash! If you are using 'single' speech marks around the format string, then you only need one backslash.
For instance, to create a string like "Y2014M01D29T1633", you *could* use string concatenation like so:
$dtstring = "Y" . date("Y", $when) . "M" . date("m", $when) . "D" . date("d", $when) . "T" . date("Hi", $when);
but you could also escape the letters with backslashes like so:
$dtstring1 = date('\YY\Mm\Dd\THi', $when);
$dtstring2 = date("\\YY\\Mm\\Dd\\THi", $when);
This method involves fewer function calls, so probably is slightly quicker; and also is immune to race conditions if you are not specifying the second argument. [It's possible that you could evaluate date("d") just *before* midnight and date("H") just *after* midnight. This will not give you the result you were expecting.]