This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Dates and Timestamps


PHP has some amazing and powerful features. In this class, we're going to go ober date(), time(), mktime(), and gmmktime().
A couple things before we go on..
If you're unfamiliar with the term timestamp, it is the time at which an event is recorded.
And the $format is displayed by case-sensitive letters. Don't worry about remembering them, you can reference back to them at the PHP.net date() reference

date()

Syntax:

date($format [,$timestamp]);

$timestamp is optional. If it is left blank, PHP will use the current time (the current server time).
$format is not optional. It must have a value. This is where the case-sensitive letters I mentioned come in.

The best way to learn the date() function is to use it, and play with it!
If you don't already have another window open, it might be helpful to have the date reference open. Click here to open it.

Example:

<?=date("M j, Y - g:i:s a");?>

Displays:

Aug 12, 2011 - 3:05:00 am

First of all, what the example above displays will be different. It's up-to-date to the second. That's the time I wrote this class.
Let's break it down.

  • Open shorthand php echo
  • Insert the date() function
  • Add "Month day, Year - Hour:Minute:second am/pm"
  • Close PHP

The date() function uses all letters. So adding a colin, comma or dash is acceptable. PHP won't try to change that into a date.
There are hundreds of different ways to display the date() function, simply check the php reference for a list of all the formats.

time()

Syntax:

time()

There is nothing that goes between the two brackets.
The time() function simply returns the current server time.

Example:

<?=time();?>

If you look at this, it displays 10 digits. What is this?! That's a timestamp. This is how PHP reads time. The timestamp is in seconds starting from Unix Epoch, which is January 1st, 1970 at 00:00:00 GMT.
How is this useful? Well.. We can now create a timestamp any time we want!

time() = Right now
time() + 60 = 60 seconds into the future
time() + 60*5 = 5 minutes (5x60 seconds) into the future
time() + 60*60*24*31 = 31 days into the future
etc, etc..

Still, this is pretty useless if we keep it as a simple integer. Let's look how we can integrate this into date()

<?

$tomo = time() + (60*60*24); // tomorrow
echo date("M j, Y - g:i:s a", $tomo);

?>

Displays:

Aug 13, 2011 - 3:05:00 am

It displayed the same time, but one day into the future.

mktime()

Syntax:

mktime([$hour, $minute, $second, $month, $day, $year, $dst]);

Every parameter in mktime() is optional. If you use mktime() without any parameters, it's the exact same as time() and will display the current server time. This function accepts input from the date() function. The date() function must return an integer, or you can feed it an integer (Names and letter characters cause errors). It will return a timestamp to you, and we use it in teh date() function. If we leave out any parameters, PHP uses the current value. Lets look:

<?

$yesterday = mktime(0, 0, 0, date("m"), date("d")-1);
# 0 Hour, 0 Minute, 0 Second, Current month, Current day minus 1.
# Year and DST were left out.
# PHP uses the current year by default
# PHP tries to figure out if it's daylight savings time right now, or not.

echo $yesterday . "<br />" . date("M j Y", $yesterday);

?>

This will display a timestamp on the first line, and "Aug 11 2011" (The day before this was written) on the second line.
We can also feed it direct dates. A timestamp for Christmas day, 2004 would look like:

$xmas = mktime(0, 0, 0, 12, 25, 2004);

So why use mktime? Because we can create a timestamp for any date.

gmmktime()

This function is the exact same as mktime(), however it returns a timestamp for GMT.
You may never use this.. You might use this.. It depends on the application you're going to create. It's good to know, nonetheless.
Syntax:

gmmktime([hour, minute, second, month, day, year, dst])

Instead of using date(), we are going to use gmdate(). We can use the same formating for gmdate() as we can for date().
Example:

<?=gmmktime(gmdate("H"), 0, 0, gmdate("m"), gmdate("d"), gmdate("Y")-2);?>

Displays:

1250071200

More:

<?=date("M j Y g:i:s a"), gmmktime(gmdate("H"), 0, 0, gmdate("m"), gmdate("d"), gmdate("Y")-2));?>

Displays:

Aug 12 2009 6:00:00 am

The time returned to you is not your local server time, it is GMT time. Please remember that when using this function.

Your task:

  1. Use the date() function to echo your birthday
  2. Use the time() function together with date() to display 10 hours ago
  3. Use mktime() to create a timestamp for the day you were born, then use date() to echo a real date (not just an integer)

Task Discussion


  • Tynanh   March 30, 2013, 6:19 p.m.

    You can check out my date_time code here

    date_time.php

    Also made a simple date calculator that calculates the approx difference of years, months, days etc, from the current time based off a user input time. See here:

    date_difference_calculator.php

  • Lisa   March 30, 2013, 6:20 p.m.
    In Reply To:   Tynanh   March 30, 2013, 6:19 p.m.
    I am out of the office on Friday, March 29th. I'll respond to your message when I return on Monday. Thanks, Lisa This email and any attachments may contain confidential and proprietary information of Blackboard that is for the sole use of the intended recipient. If you are not the intended recipient, disclosure, copying, re-distribution or other use of any of this information is strictly prohibited. Please immediately notify the sender and delete this transmission if you received this email in error.
  • katpal   July 17, 2012, 4:25 p.m.

    Hi all,

    I cannot seem to find a way to display my bday with DATE only.
    I have done it with mktime though. See my code below:

    <?
    
    //my Tasks
    
    $t= time();
    echo '<br><hr><b> My bday with DATE timestamp </b><br>';
    
    //?? what is the way to do this?
    
    echo '<br><hr><b> My bday with MKTIME </b><br>';
    
    $bday = mktime(23, 30, 0, 04, 24, 1973);
    echo date("M j, Y - G:i:s a", $bday)."<br>";
    
    
    echo '<br><hr><b> Time 10 hours ago </b><br>';
    $past=$t-60*60*10;
    echo date("M j, Y - G:i:s a", $past);
    
    
    echo '<br><hr><b> calculating age </b><br>';
    
    $year= 60*60*24*365;
    
    function calcNeg($t, $bday){
        //$neg= ($t - $bday);
        //echo $neg. " = is it really negative?<br>";
        return $t +($bday * -1);        
    }
    
    $dif=($bday < 0) ? calcNeg($t, $bday) : $t - $bday;
    
    $age=floor($dif/$year);
    
    echo "<br> your age is: ".$age;
    
    ?>