Tutorial: PHP function handyDate()

Last month it happened again, I had a friend asking "How can I easily show yesterday's date in PHP?", so I thought I would help her out and write this tutorial so everybody benefits from it.

This is a PHP function you can easy use within your existing or new PHP powered page(s). It helps you to easily get the date from yesterday, today, or tomorrow. And optionally you can provide a valid date method. This script could be a lot more complex, but it's very handy to just say "Give me the date for tomorrow", and get it in a string to use within your script. Bonus, you can also use it for the dynamic copyright year in the footer.

First of all, I have grouped everything under a single function. Called 'handyDate', and if you don't provide any variables it will simply spit out today's date in a default method.

Secondly, each type of date is in friendly English, such as yesterday, today, tomorrow, or copyright. And like I said, if nothing is provided for the type, it will assume you want today's date, which you will also get if you use an invalid type. This is handyType.

And finally, if no value is provided for the method, it will display the date in a specific method. You can check on php.net/date to see what your method options are. This is the handyMethod.

The above three terms look like this:
function handyDate($handyType = "", $handyMethod = "Y-m-d")
and to most basic form to use the function is like this:
echo handyDate();
which will output: (which is today's date at the time of writing this blog)
2012-07-29

I am using mktime, and use it over a few alternatives, but I don't really want to go into that. You're free to rewrite the function to your own liking of course:
$handyDate = mktime(0,0,0,date("m"),date("d"),date("Y"));

And I am using an if-else conditional to set the right value for $handyDate, which is used in the return statement.

Let's start with our function:
function handyDate($handyType = "", $handyMethod = "Y-m-d")
{
 // your code here
}


And before we go through the different types, let's check if we have a method first:
/*  $handyMethod  */
if ($handyMethod !== "Y-m-d") 
{
    $handyMethod = htmlentities($handyMethod, ENT_QUOTES);;
}

Which basically means: If the $handyMethod is not Exactly "Y-m-d", then we set it, but convert all applicable characters to HTML entities first. So now if something is provided, we have $handyMethod populated, and if not, we have our default. Later at the end of the function in the return statement, we're wrapping $handyDate in the PHP date() function to format it to our desired method:
return date($handyMethod, $handyDate);


At the moment the function now looks like this:
function handyDate($handyType = "", $handyMethod = "Y-m-d")
{
    /*  $handyMethod  */
    if ($handyMethod !== "Y-m-d") 
    {
        $handyMethod = htmlentities($handyMethod, ENT_QUOTES);;
    }

    // your if-else conditionals here

    return date($handyMethod, $handyDate);
}


Let's think about the different situations. If we want today's date, we can either leave it empty, or use 'today'. Yesterday is the same code, but the date("d") has to be -1, and tomorrow's date +1. But we could also ask for the copyright, which is the current year, and then the method has to be customised to just be date("Y"). And of course, we can do something if it is any other situation (for when things, such as the type, is invalid, or not recognised).

Let's start by checking if the type is empty, or set to today.
if ($handyType == 'today' OR empty($handyType))
{
    $handyDate = mktime(0,0,0,date("m"),date("d"),date("Y"));
}


We can re-use the above code, and just -1 or +1 the 'day' part.
elseif ($handyType == 'yesterday')
{
    $handyDate = mktime(0,0,0,date("m"),date("d")-1,date("Y"));
}
elseif ($handyType == 'tomorrow')
{
    $handyDate = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
}


Before we check for anything else, we also want to get today's date for the dynamic copyright year, but use a custom method so we only get the current year and not the full date.
elseif ($handyType == 'copyright')
{
    $handyDate = mktime(0,0,0,date("m"),date("d"),date("Y"));
    $handyMethod = "Y"; // overwrite
}


And finally, if nothing matches, or basically if the type is wrong, we default to today's date.
else
{
    $handyDate = mktime(0,0,0,date("m"),date("d"),date("Y"));
}


And now our function looks like this, all done and put together:
function handyDate($handyType = "", $handyMethod = "Y-m-d")
{
    /*  $handyMethod  */
    if ($handyMethod !== "Y-m-d") 
    {
        $handyMethod = htmlentities($handyMethod, ENT_QUOTES);;
    }

    /*  $handyType  */
    if ($handyType == 'today' OR empty($handyType))
    {
        $handyDate = mktime(0,0,0,date("m"),date("d"),date("Y"));
    }
    elseif ($handyType == 'yesterday')
    {
        $handyDate = mktime(0,0,0,date("m"),date("d")-1,date("Y"));
    }
    elseif ($handyType == 'tomorrow')
    {
        $handyDate = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
    }
    elseif ($handyType == 'copyright')
    {
        $handyDate = mktime(0,0,0,date("m"),date("d"),date("Y"));
        $handyMethod = "Y"; // overwrite
    }
    else
    {
        $handyDate = mktime(0,0,0,date("m"),date("d"),date("Y"));
    }

    return date($handyMethod, $handyDate);
}

How to use?

If your own script has a functions file, you can add the above function to it. And then in any location in your script you can use something like echo to call the function and have it print out the result. A few examples:
echo handyDate(); // should echo out in Y-m-d method, today's date.
echo handyDate('fake'); // since this is invalid, defaults to today's date.
echo handyDate('yesterday'); // yesterday's date
echo handyDate('today'); // today's date
echo handyDate('tomorrow'); // tomorrow's date
echo handyDate('copyright'); // the current year, method is ignored, and will be like this: 2012
echo "Tomorrow it is ".handyDate('today','l jS \of F\, Y'); /* prints: Tomorrow it is Sunday 29th of July 2012 */


You can find more examples, and the latest version of the script here: http://mrfloris.com/handyDate/

I hope this script helps you get started, feel free to customise it to your needs, or to extend it with last month, next year, etc. This basically serves as a starting template, kept simple so it makes sense in a tutorial. Feel free to make suggestions.

Next Article: The best Apple device to buy