Pin Me

Understanding the PHP Date Function

written by: Strobe•edited by: Michele McDonough•updated: 6/30/2011

PHP provides a very useful function for dealing with dates. This function is the php date function and in this tutorial we’ll show you how to make the most of this function.

  • slide 1 of 1

    Whenever you’re making a web application, you will need to use dates in your PHP script. Although not a necessity, it’s likely that you’ll be using it. This tutorial looks at the PHP date function and how it’s used when created PHP scripts.

    Adding the PHP date function to your PHP scripts can aid in the aesthetics of your PHP script and help it become more user friendly by giving them more information. First we’ll look at the format of the date function and how we call it in our PHP scripts.


    $mydate = date(“m-d-Y”);

    echo $mydate;


    As you can see, this PHP script is very small. But, even in its size it has huge potential and can be manipulated to do some incredible things with the current date. We’ll go over what the PHP script is actually doing. In the first line, we’re just opening a PHP tag, letting the interpreter know that what follows will be PHP code. On the next line we set up variable called “mydate” which will hold the actual date that is returned by the date function. Note that the arguments sent to the date function “m-d-Y” which tells the date function how we want the format of the output of the PHP date function to be returned. And the next line just prints out our variable “mydate” which holds the results of the PHP date function. Then finally the closing tag for the PHP script.

    The reason this PHP script is so powerful is because we can format the date using the arguments sent to the PHP date function. Below are a list of arguments that the PHP date function will take.

    a – this is to display "am" or "pm"

    A – Same as above but caps, "AM" or "PM"

    d – The current day of the month, 2 digits with leading zeros; i.e. "01" to "31"

    D – The current day of the week, textual using 3 letters; i.e. "Fri"

    F - Month, textual, long; i.e. "January"

    h – The current hour in 12 hour format (01 to 12)

    H – Same as above but 24 hour format (0 to 23)

    g – The current hour with the leading zeros in 12 hour format (1-12)

    G – Same as “g” but in 24 hour format without leading zeros

    i – The current minutes

    j – The current day of the month without leading zeros

    l – The current day of the week textual using long format, i.e. "Friday"

    L - Boolean for whether it is a leap year; i.e. "0" or "1"

    m – The current month (01-12)

    n – The current month without leading zeros (1-12)

    M – The current month using 3 letters. (Jan)

    s – The current seconds (0-59)

    Y – The current year using four digits (2008)

    y – The current year using two digits (08)

    z – The current day of the year (0-365)

    Z – The current time zone offset in seconds (i.e. "-43200" to "43200")

    These are some to the arguments that PHP date function will accept. You can try out different ones with different combinations by plugging them in to the simple PHP script that is listed above. Using this PHP date function is pretty powerful and can help your web site visitor in many ways.