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
$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.
<?=date("M j, Y - g:i:s a");?>
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.
There is nothing that goes between the two brackets.
The time() function simply returns the current server 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
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);
Aug 13, 2011 - 3:05:00 am
It displayed the same time, but one day into the future.
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.
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.
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().
<?=gmmktime(gmdate("H"), 0, 0, gmdate("m"), gmdate("d"), gmdate("Y")-2);?>
<?=date("M j Y g:i:s a"), gmmktime(gmdate("H"), 0, 0, gmdate("m"), gmdate("d"), gmdate("Y")-2));?>
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.
- Use the date() function to echo your birthday
- Use the time() function together with date() to display 10 hours ago
- Use mktime() to create a timestamp for the day you were born, then use date() to echo a real date (not just an integer)