Forgot Password / Register
Site Statistics
Total Members: 520
Total Tutorials: 242
Newsest User: 8884244477
Todays Unique Hits: 515
0 Users 5 Guests Online

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
2
3
4
5
<?php
$r 
= ("m/d/y"); //returns: 03/21/2007
echo "$r"//echos date
?>


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:

Code

date("F j, Y, g:i a");
date("m.d.y");
date("j, n, Y");
date("D M j G:i:s T Y");
date("H:i:s");
date('\i\t \i\s \t\h\e jS \d\a\y.');


Which Would Return The Following:
Code

- March 10, 2001, 5:16 pm
- 03.10.01
- 10, 3, 2001
- Sat Mar 10 15:16:08 PST 2001
- 17:16:17
- 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
Author:
Views:
2374
Rating:
Posted on Tuesday 26th February 2008 at 07:50 PM
ShadowMage
ShadowMage
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
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
you could always do:
Code

<?PHP
$date = date('jS');
echo("It is the $date day.);
?>