Date + Time


When developing a website with PHP it is often beneficial to report server times and/or dates. On its own this ability is quite useful, but in combination with further scripting, it allows you to account for time zones, datelines, etc, thus improving the overall user experience. The popular forums software Vbulletin, for example, supports these functions natively. Best of all, you can output the current server date/time with the following simple PHP instruction:

An example is:
PHP Code
  1. <?php
  2. $r = ("m/d/y"); //returns: 03/21/2007
  3. echo "$r"; //echos date
  4. ?>


Now for the functions

- A = Uppercase 12hr time syntax eg. PM
- a = Lowercase 12hr time syntax eg. pm
- D = Day eg. Fri
- d = Day eg. 03
- F = Full Text Month eg. January
- G = Hours in 24 hour format without leading zeros eg. 17
- g = Hours in 12 hour format without leading zeros eg. 7
- H = Hours in 24 hour format with leading zeros eg. 17
- h = Hours in 12 hour format with leading zeros eg. 07
- i = Minutes eg. 29
- M = Month eg. Jan
- m = Month eg. 01
- O = GMT time difference in hours eg. +0400
- S = Day of month suffix eg. st, nd, rd, or th.
- s = Seconds eg. 28
- T = Timezone setting eg. GMT, PST, EST, etc
- t = Total number of days in the month eg. 28, 31, etc.
- U = Seconds since the UNIX epoch eg. 1041604168
- W = Week number in year eg. 42 (42nd week in the year)
- w = numeric representation of the day eg. 1 = monday, 2 = tuesday, etc.
- Y = Year eg. 2003
- y = Year eg. 03
- z = Day of the year eg. 002

A Few Simple Codes:

PHP Code
  1. date("F j, Y, g:i a");
  2. date("m.d.y");
  3. date("j, n, Y");
  4. date("D M j G:i:s T Y");
  5. date("H:i:s");
  6. date('\i\t \i\s \t\h\e jS \d\a\y.');


Which Would Return The Following:
PHP Code
  1. - March 10, 2001, 5:16 pm
  2. - 03.10.01
  3. - 10, 3, 2001
  4. - Sat Mar 10 15:16:08 PST 2001
  5. - 17:16:17
  6. - It is the 10th day. *


* = Don't forget to de-limit any normal letters you want to include with the \ symbol, or else the date/time function will interpret the values as intended date/time syntax.
ShadowMage's Avatar
Author:
Views:
2,657
Rating:
Posted on Tuesday 26th February 2008 at 07:50 PM
ShadowMage
ShadowMage's Avatar
Yes there is. you could use set_default_timezone('3 DIGIT TIMEZONE CODE'); ;)
Posted on Tuesday 26th February 2008 at 01:32 PM
Nathan
Nathan's Avatar
is there anyway you could make it in california time if i lived in australia?
Posted on Tuesday 27th March 2007 at 04:30 PM
MOD-Dan
MOD-Dan's Avatar
you could always do:
Code
<?PHP
$date = date('jS');
echo("It is the $date day.);
?>