1. Computing

Discuss in my forum

Date & Time in PHP

By , About.com Guide

See More About
The first thing you need to know about timestamps is that all time is measure in the amount of time the has passed since January 1st, 1970 at midnight GMT. When you created a time stamp you will get a string of numbers that may look meaningless to you, but it represents how many seconds have elapsed since the date I mentioned. Fortunately you can not only use PHP to timestamp, but also to interpret the time stamp into a more user readable format. You can create a timestamp for the current second, and you can also create a timestamp for any date in the future or past.

First you need to learn how to use the time () function. The time function is what generates the seconds for the current time. Let's try that here:

<?php 
 $s = time (); 
 print $s; 
 ?> 
As you can see this is an unformatted string of numbers and although it is useful for record keeping it isn't great if it's meant to be read by humans. That is where the date () function comes into play. The date function is used to format the time function. You use it like this: date (format, time). But before you can do that, lets have a look at some of the formatting options:

a - "am" or "pm"
A - "AM" or "PM"
d - day of the month, 2 digits with leading zeros; "01" to "31"
D - day of the week as text, 3 letters; "Mon"
F - month as text, full name; "January"
h - hour, 12-hour format; "01" to "12"
H - hour, 24-hour format; "00" to "23"
g - hour, 12-hour format without leading zeros; "1" to "12"
G - hour, 24-hour format without leading zeros; "0" to "23"
i - minutes; "00" to "59"
j - day of the month as a number without leading zeros; "1" to "31"
l (lowercase L) - day of the week as text, full name; "Monday"
L - boolean for if it is a leap year; "0" or "1"
m - month as a number; "01" to "12"
n - month as a number without leading zeros; "1" to "12"
M - month as text, 3 letters; "Jan"
s - seconds; "00" to "59"
S - English ordinal suffix as text, 2 characters; "th", "nd"
t - number of days in the given month; "28" to "31"
U - seconds since the epoch
w - day of the week as a number; "0" (Sunday) to "6" (Saturday)
Y - year, 4 digits; "1999"
y - year, 2 digits; "99"
z - day of the year; "0" to "365"
Z - timezone offset in seconds ("-43200" to "43200")

Now that you know some formatting options, let's try to actually using the date function:

<?php 
 $s = time (); 
 print date("m/d/y",$s) . "<br>"; 
 print date("D, F jS",$s) . "<br>"; 
 print date("l, F jS Y",$s) . "<br>"; 
 print date("g:i A",$s) . "<br>"; 
 print date("r",$s) . "<br>"; 
 print date("g:i:s A D, F jS Y",$s) . "<br>"; 
 ?> 
You can also create a timestamp for a future (or past) date. Let's say you want the date to be Christmas in the year 2020. You do this using mktime which is written like this: mktime ( hour, minute, second, month, day, year, is_dst)

Let's use mktime () in our previous example where we use date ():

"; 
 print date("D, F jS",$s) . "<br>"; 
 print date("l, F jS Y",$s) . "<br>"; 
 print date("g:i A",$s) . "<br>"; 
 print date("r",$s) . "<br>"; 
 print date("g:i:s A D, F jS Y",$s) . "<br>"; 
 ?> 

As you can see once you have created the timstamp with mktime () you can manipulate it with date like any other time stamp.

  1. About.com
  2. Computing
  3. PHP / MySQL
  4. Learn PHP
  5. Time & Date in PHP

©2013 About.com. All rights reserved.