Wednesday, June 6, 2012

PHP tutorial: Displaying Date and Time


PHP tutorial: Displaying Date and Time

Displaying Date and Time

The date and time we will show how to display in this tutorial is the one specified by the server which is hosting our pages. In case you want to display a different date or time (p. e.,  your clients are mostly from Belgium but your server is located in US and you want to display the local time in Belgium) you will find how to do it  latter on this page.

In the table bellow we have include the PHP code necessary to display one by one all the time and date related information. By copying the code in the first column to your page you will get the data which is explained in the third column. The  column in the middle is the value of those data the day we were preparing this page.   

Code Output  
<?php print  date("a"); ?> pm "am" or "pm"
<?php print  date("A"); ?> PM "AM" or "PM"
<?php print  date("d"); ?> 15 Day of the month: 01 to 31
<?php print  date("D"); ?> Tue Day of the week: Sun, Mon, Tue, Wed, Thu, Fri, Sat
<?php print  date("F"); ?> October Month: January, February, March...
<?php print  date("h"); ?> 03 Hour: 01 to 12
<?php print  date("H"); ?> 15 Hour: 00 to 23
<?php print  date("g"); ?> 3 Hour: 1 to 12
<?php print  date("G"); ?> 15 Hour: 0 to 23
<?php print  date("i"); ?> 26 Minutes: 00 to 59
<?php print  date("j"); ?> 15 Day of the month: 1 to 31
<?php print  date("l"); ?> Tuesday Day of the week: Sunday, Monday, Tuesday...
<?php print  date("L"); ?> 0 Is it a leap year? 1 (yes) or 0 (no)
<?php print  date("m"); ?> 10 Month: 01 to 12
<?php print  date("n"); ?> 10 Month: 1 to 12
<?php print  date("M"); ?> Oct Month: Jan, Feb, Mar, Apr, May...
<?php print  date("s"); ?> 03 Seconds: 00 to 59
<?php print  date("S"); ?> th Ordinal: 1st, 2st, 3st, 4st... Need to be used with a numeric time/date value. See latter.
<?php print  date("t"); ?> 31 Number of days in the month: 28 to 31
<?php print  date("U"); ?> 1034691963  Seconds since 1970/01/01 00:00:00
<?php print  date("w"); ?> 2 Day of the week: 0 (Sunday) to 6 (Saturday)
<?php print  date("Y"); ?> 2002 Year (four digits)
<?php print  date("y"); ?> 02 Year (two digits)
<?php print  date("z"); ?> 287 Day of the year: 0 to 365
<?php print  date("Z"); ?> -21600 Difference in seconds from Greenwhich meridian

As shown in the table the commands we are using in all case are "print" (in order to show the values to the visitor) and "date" (which will allow us to get the data corresponding to the string code we are using between brakets).

So we already know how to obtain the data and how to show it  in our page, but if we want to display different values simultaneously, we have at least three option:


The code
Output
<?php print  date("Y"); ?>:<?php print  date("m"); ?>: <?php print  date("d"); ?> 2002:10:15
<?php print  date("Y").":".date("m").":".date("d"); ?>
2002:10:15
<?php print  date("Y:m:d"); ?>
2002:10:15

The first option is very easy to understand (we have just copied the code from the table one by one). The second option concatenates the data basically in the same way, and the third one is probably the most useful system, but the one we must understand before using it.

Command  "date" will get the data we want to display, and that data is specified by the string used within data (in our case: "Y:m:d"). Each character in this string may or may not have a meaning depending upon there is or there is not a value asociate with that letter (see the first table in this page). In our case some characters will be replaced by its corresponding value:


Y
:
m
:
d      
Year (four digits)
no meaning
Month: 01 to 12
no meaning
Day of the month: 01 to 31

Check this usefull examples:

The code
Output
<?php print  date("Y:m:d H:i") ?> 2002:10:15 15:26
<?php print  date("l dS of F Y h:i:s A"); ?>
Tuesday 15th of October 2002 15:26:03 PM
The time is <?php print  date("H:i") ?>.
That means it's <?php print  date("i") ?>
minutes past <?php print  date("H") ?> o'clock.

The time is 15:26. That means it's 26 minutes past 15 o'clock.


Take care when using date command or you may get unwanted data as shown in the first row in the table bellow (use the code in second row instead):

The code
Output
Character with meaning
<?php print  date("Today is l"); ?> WETo15pm02 2603 Tuesday The following characters have a meaning: T, d, a, y, i, s,  l
<?php print  "Today is ".date("l"); ?>
Today is Tuesday Only data asociated to "l" (day of the week) is requested


Example: Link of the Day

What if you wanted to have a link that points to a different page every day of the week? Here's how you can do that. First, create one page for each day of the week and name them "Sunday.htm," "Monday.htm," and so on. 

To make the link, copy the code bellow to your page

<a href= <?php print  date("l"); ?>.htm>Link of the Day</a>


Place the code in your ".php" page where you want it to appear. When you click this link in your browser, it will take you to the "Link of the Day".

Using "S" with date comand.

Lets suppose we are using the code bellow in different consecutive days:


Day
Code
Output
2002/01/01
<? php print  date("nS of F"); ?>
1st of January
2002/01/02
<? php print  date("nS of F"); ?>
2nd of January
2002/01/03
<? php print  date("nS of F"); ?>
3rd of January
2002/01/04
<? php print  date("nS of F"); ?>
4th of January
The "S" character included within command date will allow us to show "st", "nd", "rd" or "th" values depending on the number preceding the character "S".


Displaying local time

In this tutorial we will consider our server is located in a different time zone from the one our clients are located at (a Belgium related site is in a server located is USA for example).

First we must know the time in the server. We will create a text file with the code bellow, and we will copy it to our server:

Time in server: <?php print  date("H:i") ?>

Then we will visit our page and we will get the time in the server. Let suppose the time is 16:00

Second, we will calculate the difference in hours between local time and the time in server. Let suppose the time in Belgium is 20:00, so the difference is 4 hours.

To get the local time we will use the code in the table bellow:

<?php
$differencetolocaltime=4;
$new_U=date("U")-$differencetolocaltime*3600;
print date("H:i", $new_U);
?>
Lets explain this code:
  • We have create a variable named $differencetolocaltime, and we have stablish the value for this variable (4)
  • In third line of the script we have create a variable named $new_U, and the value for this variable will be 'date("U")' (Seconds since 1970/01/01 00:00:00) to which we have substracted the difference of hours between the two time zones (in our case 4 hours, which is the value for the variable $differencetolocaltime, has been multiplied by 3600, which is the number of seconds in one hour)
  • In the last step we have write to the document the new hour and time by using "date" command to which we have let know the exact date (specified by $new_U) from which we want to get the time (if it is not specified, as for example when using 'date("H:i")', the time and date in the server will be displayed as shown before in this page). 


1 comment:

There was an error in this gadget