Using PHP to generate CSS

A neat and dirty trick for easier CSS manipulation, but you may say is a bit harder to develop CSS this way. My idea was that I can create a PHP file, which will add some more dynamics to your CSS. By that I mean you can add variables such as:

$mainColor = '#123';
$secondaryColor ='"#222';
$fontFamily = 'Georgia';
$fontSize = '12pt';
$backgroundColor = '#f5f5f5';

and use them in your CSS like this:

body
{
    font-family: < ?php echo$fontFamily; ?>;
    font-size: < ?php echo $fontSize; ?>;
    color: < ?php echo $mainColor; ?>;
    background-color: < ?php echo $backgroundColor; ?>;
} 

/* and many, many more elements */

Now you might guess how easy it can be to leave the same layout, but make a completely different look of the application, just by modifying a few variables.

More on generating CSS styles with PHP…

Javascript Date Formatter

There is little (actually there is absolutely no) support in Javascript for formatting Date objects in a different formats when converting them to string, and for parsing dates from strings in different formats.

I have created a little “patch” to fix this issue, and to add the formatting and formatted parsing abilities to the Javascript Date object. This patch will add the following functions:

  • Date.format(dateObject, format)
  • dateObject.toFormattedString(format)
  • Date.parseFormatted(value, format)
  • dateObject.fromFormattedString(value, format)

The patch is called Javascript Date Format Patch, and it’s available on my github profile. Javascript Date Format Patch is dual licensed under the MIT and GPL licenses.

Format Description

When converting Date object to string, you can use any of the following format expressions, combined with other characters to get the output result you wish.

Format expression   Result
d the day as number without a leading zero (1 to31)
dd the day as number with a leading zero (01 to 31)
ddd the abbreviated localized day name (e.g. ‘Mon’ to ‘Sun’)
dddd the long localized day name (e.g. ‘Monday’ to ‘Sunday’)
M the month as number without a leading zero (1-12)
MM the month as number with a leading zero (01-12)
MMM the abbreviated localized month name (e.g. ‘Jan’ to ‘Dec’)
MMMM the long localized month name (e.g. ‘January’ to ‘December’)
yy the year as two digit number (00 to 99)
yyyy the year as four digit number
h the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hh the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
H the hour without a leading zero (0 to 23, even with AM/PM display)
HH the hour with a leading zero (00 to 23, even with AM/PM display)
m the minute without a leading zero (0 to 59)
mm the minute with a leading zero (00 to 59)
s the second without a leading zero (0 to 59)
ss the second with a leading zero (00 to 59)
z the milliseconds without leading zeroes (0 to 999)
zzz the milliseconds with leading zeroes (000 to 999)
AP use AM/PM display. “AP” will be replaced by either “AM” or “PM”
A same as “AP”
ap use am/pm display. “ap” will be replaced by either “am” or “pm”
a same as “ap”

More on formatting and parsing dates in Javascript…