Working with dates



Instead of having to manually create a function that displays date information from numbers (like you might have been used to doing back in C++ class) PHP offers a function for this. The function date() will become your best friend when working with time in a PHP project.

date() takes one or two arguments, depending on if you want to display the current date/time or a specific date/time. The first argument is the format string which controls how the outlook will appear. The second (optional) argument is the date that you want to have displayed.

character

Description

return values

Day

---

---

d

Day of the month, 2 digits with leading zeros

01 to 31

D

A textual representation of a day, three letters

Mon to Sun

j

Day of the month without leading zeros

1 to 31

l (lower 'L')

Textual representation of the day of the week

Sunday to Saturday

N

numeric representation of the day of the week

Mon=1, Sun=7

S

English suffix for the day of the month, 2 letters

st, nd, rd or th. (use with j)

z

The day of the year (starting from 0)

0 to 365

Month

---

---

F

Textual representation of a month

January to December

m

Numeric month, with leading zeros

01 to 12

M

Short textual representation of a month, three letters

Jan to Dec

n

Numeric month, without leading zeros

1 to 12

Year

---

---

Y

Year expressed in 4 digits

e.g. 1987 or 2007

y

Year expressed in 2 digits

e.g. 87 or 07

Time

---

---

a

Lowercase AM / PM

am or pm

A

Uppercase AM / PM

AM or PM

g

12-hour format of an hour without zeros

1 to 12

G

24-hour format of an hour without zeros

0 to 23

h

12-hour format of an hour with zeros

01 to 12

H

24-hour format of an hour with zeros

00 to 23

i

Minutes with zeros

00 to 59

s

Seconds, with zeros

00 to 59

There are more formatting characters used for the format string of the date function but these are the most common ones. This table was taken from php.net/date.

Let's say that you want to print the current date. What you would do is:

echo date('M t, Y');
?>

The result of this function would be Oct 31, 2006. The M is short month, the t is day of month, and the Y is the year. The spaces and comma are printed because they are not formatting characters (One server I used didn't accept 'Y' but instead only took 'o' for the year so the output for the year was a capital Y).

Now, if you want to print a specific date, you would do this:

echo date('M t, Y', $my_date);
?>

Now, the format for the specific date is some weird UNIX timestamp, which I believe is in milliseconds from the beginning of UNIX time (being in 1970). This format isn't the same format that you get from, let's say, a MySQL timestamp record. If you take a timestamp from MySQL and put it directly into the second argument for the function, the result will be sometime in the 1970's. This is my solution I am using for this CMS:

echo date("F j, Y g:ia ", strtotime($row['date']));

outputs October 29, 2006 1:07am

strtotime() is a native PHP function that takes a wide range of string inputs that represent a date and properly output a UNIX timestamp. The function is quite smart and pretty much any timestamp format you throw at it (HH-MM-SS, YYYY-MM-DD-HH-MM-SS, etc) will work properly.

Tutorial Added Oct 29, 2006 @ 1:10am

Login

Username:
Password:

Shoutbox

By !nucleo
Jun 12, 2008 7:56pm
About time you're back... Shoot me an email with your 88x31.
By ?Lee
Jun 12, 2008 3:27pm
vimixx.net has moved to lee-stewart.co.uk!!!
By ?Medvedko
May 4, 2008 7:04am
That's right. Sorry, I am not making much sense, am I? Basically, I created the DB myself instead of using your install script. All necessary tables are there, I added few more based on plugin POST options and added those to the update.php also. At the beginning I added mysql.ssi.php function to both update and winamp_playing scripts as I had some troubles before with include_once.
By !nucleo
May 2, 2008 9:56am
Now I'm really confused, are you modifying my shoutbox script?
By ?Medvedko
Apr 30, 2008 1:08am
another thing is that update script terminates with invalid password (I added the pass in the Additional Options in the plugin and set the pass in the update script.
By ?Medvedko
Apr 30, 2008 1:06am
my fault really. I modded the scripts and added more tables to the DB such as "TIMESTAMP". I think include_once doesn't fetch the mysql function. Or atleast this is what seems to me is the problem.
Name: Shout: This Number: [View All]

Partners

Rand Affiliates

Sevenz.net Cryostir ObsceneArt.net Leeackerman