Go to the menu - Go to the contents

Date and time


Tutorial's information

Avatar
Author : M@teo21
Nb of visits : 3 230


More information More information
After this Guest book practical, we're going to have a break with something cool: date and time with PHP.

This chapter is made of 2 parts:

  • First, we'll deal with the date function's possibilities, to retrieve the day, the month etc. Yeah, I know we already talked about it in Part I. It was to show you an example of a ready to use function in PHP. But here, we will go through all the possibilities in detail.
  • Then, we'll see the timestamps, which will be quite useful to make calculations on dates!


Moreover, I'll tell you something that will cheer you up: if you can handle the dates, you'll be qualified to set about a practical "news system"! :D
Chapter Contents
Chapter Icon
Previous Chapter Contents Next Chapter

Date function

Almost incontrovertible when you want to use a date in PHP.
Actually, date is a multi-purpose function: if you can use it properly, you'll be able to display any date element in all the calenders of the entire world ^^


We will base ourselves on this example to see how it works:

Code: PHP
1
2
3
4
<?php
$day = date('d');
echo 'Today, is the: ' . $day;
?>




We give a letter to the function date. The result that is returned depends on the letter given to the date.
Here, I have put "d": it means "Return the day's number".

But... How could I guess that "d" stands for "Return the day's number"?


You could not guess, indeed :p

date can return a lot of different values, I won't list them all because some of them will be of no use to you.
I give you a good quantity for now, and the corresponding meaning:
The Example column contains the values as they appeared on Monday 29th of August 2005 at 0h26.


LetterMeaningPossible valuesExamples
s Seconds 00 to 59 53
i Minutes 00 to 59 26
H Hour 00 to 23 00
A Ante meridiem or Post meridiem AM or PM AM
I Indicates if the Summer Time is activated (1 = yes, 0 = no) 0 or 1 1
O Difference in hours with GMT (Greenwich) -1200 to +1200 +0200
d Day of the month 01 to 31 29
m Month of the year 01 à 12 08
Y Year, 4 figures number Many possibilities 2005
y Year, 2 figures number Many possibilities 05
L Indicates if it is a leap year (1 = yes, 0 = no) 0 or 1 0
l Day of the week Sunday to Saturday Monday
F Month January to December August
t Number of days in the month 28 to 31 31
w Number of the day in the week 0 (Sunday) to 6 (Saturday) 1
W Number of the week in the year 01 to 52 35
z Number of the day in the year 0 to 366 240


I wanted to point out that it is not the client's hour that is returned but the server's. Moreover, the values are different according to the place your server is based ;)


This spreadsheet gives you all the pieces of information that you need to work on dates.
But you haven't seen everything yet: date can be given several letters at the same time, like this:

Code: PHP
1
2
3
<?php
echo 'Today is the: ' . date('d/m/Y');
?>




You may have been surprised that I did not use any variable for concatenation this time. Nothing wrong: I am perfectly allowed to use a function right in the middle of a concatenation like I just did.


date has created a string of characters containing day/month/year. In fact, you can put what you want in date. When the function recognizes a letter, it replaces it by the corresponding value.
That means you can put spaces, dashes, or slashes like I did to separate the date elements. We only have to call the function once, which is quite convenient ^^

That's it for the date function. There's nothing more to say, it's quite easy to use.

The timestamp

Well, until now, there was nothing really exciting. You simply saw that PHP could give all you need to display the date.
But you haven't seen the most interesting part: the timestamp.

Retrieve the timestamp



What is a timestamp ? o_O


A timestamp is a number.
It's the number of seconds that have passed since January 1st, 1970 at Midnight.

Why from January 1st, 1970? It's merely symbolic, as we had to start from somewhere.

On January 1st, 1970 at Midnight, the value of the timestamp was 0. As you can imagine, many seconds have passed today.
It is the function time (which doesn't require any parameter) that is used to get the current timestamp in PHP:

Code: PHP
1
2
3
<?php
echo 'The current timestamp is: ' . time();
?>




If you try to reload the page every second, you'll see that the timestamp doesn't stop rising. The timestamp is actually a number that grows bigger every second!

At this point, you might think that the timestamp is a funny but useless thing.
FALSE: on the contrary, it will be very useful, we'll see why...

The timestamp with the function date



date can be given another parameter (after the letters): the timestamp on which you want to obtain information.

date use the current timestamp as a default value: it returns the current time, the current day etc. But if you give it a timestamp, it will make calculations on this particular moment.

Here is a =>worldwide scoop<= the timestamp of the moment when I was writing these lines ^^

1104276413


So we have a timestamp, and we're going to extract all the data we want:

Code: PHP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?php
$timestamp = 1080513608; // The time it was when I was writing the tutorial!
?>
 
<p>Here are pieces of information on my timestamp:</p>
 
<ul>
<li>M@teo wrote those lines on <?php echo date('m/d/Y', $timestamp); ?></li>
<li>This day was a <?php echo date('l', $timestamp); ?></li>
<li>It was: <?php echo date('H:i A', $timestamp); ?> (poor insomniac!)</li>
<li>There were <?php echo date('t', $timestamp); ?> days in this month.</li>
<li>It was the <?php echo date('z', $timestamp); ?>st/th day of the year!</li>
</ul>




Don't mix things up: it is not the number 1 but a lowercase L that is used to display the day!



The difference with the beginning of this chapter is that we have given the date function a second parameter: this parameter is a timestamp. When performing:
echo date('d');
... we're telling PHP : "Display the number of the current day"

But if we add a timestamp as the second parameter:
echo date('d', $timestamp);
... this time we're telling PHP: "Display the number of the day it was at the moment of this timestamp"

This will be a very, very useful thing!
When you will write pieces of news, for instance, all you will need to do will be storing the timestamp. Thanks to this number, you will be able to retrieve all possible data on it: the day when the item of news was posted, the time it was, etc. That makes it really powerful! :D

Retrieve the timestamp from a date



At last, something that might come in handy: you would like to know the timestamp of February, 5th 1998 at 13h 45min 26s (precisely :p ).

To retrieve the corresponding timestamp, we will use the function mktime. We will give it a date as a parameter, it will give us the corresponding timestamp.
This function can take quite a number of parameters, here is the list, in order :

$timestamp = mktime(hour, minutes, seconds, month, day, year, winter time);

You can forget about the last parameter (winter time), which will generally be of no use. Note that 1 has to be put if the summer time is enabled, 0 if it isn't. But, let's forget it. If we remove the winter time to avoid confusion, there will be 6 possible parameters left :

$timestamp = mktime(hour, minutes, seconds, month, day, year);

Here is an example, to clear things up. I still want the timestamp of February 5th, 1998 at 13h45 26s (Yes, I am stubborn), I will write it in the following code.
Code: PHP
1
2
3
4
<?php
$old_timestamp = mktime(13, 45, 26, 2, 5, 1998);
echo 'The timestamp of 05/02/1998 at 13h 45min 26s was : ' . $old_timestamp;
?>




If you do not want to display the time and you want only a date from the timestamp... impossible (I remind you that there are several timestamsp in a day!!;
The widely used solution is to base on Midnight:
mktime(0, 0, 0, 2, 5, 1998);

This is the timestamp of February 5th, 1998 at Midnight. It may not be as precise as the example given above but, sometimes, we don't need that amount of precision.

This is the typical kind of chapter that I like to compose: quite easy to understand yet full of information. Besides, knowing that you're going to make use of it right now makes it even more interesting.
Yes, ladies and gentlemen, we'll now go through the practical you were all waiting for: a news system for your site! :D
Previous Chapter Contents Next Chapter

Tutorial's information

Back on top Back on top

Creation : : On 06/26/2007 10:23:18 AM
Last modification: : On 10/07/2008 01:00:23 AM
Progress : 0%
License : Unauthorized copy

Comments
Alert Validators