[DMT] Week 4 Practical (PHP)


Originally from http://kiri.dmt-uts.net

This post was originally posted here, on my DMT UTS blog, on Mar 19, 2013 @ 5:57pm.

The page produced from this exercise can be accessed here.

The original PHP code which was provided in the lab's files was:

echo "The date is: "; echo date("d m Y H:i:s");

Which printed the date in the following format:

The date is: 19 03 2013 16:44:52

The instructions provided for the tutorial were:

Show the date in the following style 'Monday 15th of August 2005 03:12:46 PM'

I consulted the PHP documentation (The PHP Group, 2013) as instructed and edited the PHP code so that it was mostly formatted in the right manner.

echo "The date is: ";
echo date("D dS of F Y h:i:s A");

However the 'of' portion of the above code returned '2013f', as the o returns the same value as Y. Thus, I looked for an escape character in order to be able to show the 'of' as plaintext, rather than have it return a value which I did not require.

This reference (The PHP Group, 2013) told me that escape characters in PHP are \, although sometimes this can be used to enter new lines, and so on. I tried this and it worked, however there is also another solution to this problem which is to split the code, however this is not as elegant. The two solutions are presented below, which displays the date in the correct format:

echo "The date is: ";
echo date("D dS \of F Y h:i:s A");
echo "The date is: ";
echo date("D dS");
echo " of ";
echo date("F Y h:i:s A");

References

The PHP Group 2013, PHP: date - Manual, viewed 19 March 2013, <http://www.php.net/manual/en/function.date.php>

The PHP Group 2013, PHP: Escape sequences - Manual, viewed 19 March 2013, <http://php.net/manual/en/regexp.reference.escape.php>