Home > Articles > Open Source > PHP

Doing the Time Warp: Manipulating Date and Time in PHP 5

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
PHP Phrasebook

This chapter is from the book
PHP Phrasebook

Most websites utilize the PHP date and time features only to display the current date, often just to make the website appear constantly up-to-date. Fortunately, though, the date and time features in PHP can be used to much greater effect, and this chapter details many of the possible uses for these handy tools.

Most of the time, the date and time functionalities of PHP 5 are used for printing out the current date and time—to pretend that the web page is up to date (you would be surprised how many larger websites actually use this). But apart from that, working with date and time in PHP offers many other possibilities, most of which you will find in this chapter.

First, though, it seems appropriate to have a look at the PHP function that is probably used the most for working with dates—date(). This function can take the current date (or an arbitrary one) and extract some information about it, for example, the day, whether it's a.m. or p.m., and what time it is according to the rather failed marketing stunt, "Swatch Internet Time." To do so, you call date() and provide a string as the first parameter. This string may now contain a list of formatting symbols that can be seen in Table 3.1 (the PHP manual carries a list with more examples at http://php.net/date). Each of these symbols is replaced by the associated date/time value.

Table 3.1 Formatting Symbols for date()

Symbol

Description

a

am or pm

A

AM or PM

B

Swatch Internet Time (between 000 and 999)

c

Date in ISO 8601 format

d

Day of month (from 01 to 31)

D

Day of week (from Mon to Sun)

F

Month (from January to December)

g

Hour (from 1 to 12)

G

Hour (from 0 to 23)

h

Hour (from 01 to 12)

H

Hour (from 00 to 23)

i

Minutes (from 00 to 59)

I

Whether date is in DST (1) or not (0)

j

Day of month (between 1 and 31)

l

Day of month (from Sunday to Saturday)

L

Whether date is in a leap year (1) or not (0)

m

Month (from 01 to 12)

M

Month (from Jan to Dec)

n

Month (from 1 to 12)

O

Difference to GMT (for example, +0100 for one hour ahead)

r

Date in RFC 2822 format

s

Seconds (from 00 to 59)

S

Ordinal suffix for the day of month (st, nr, td, th)

t

Number of days in the provided month (from 28 to 31)

T

Time zone of server (for example, CET)

U

Epoche value (seconds since January 1st, 1970, Midnight GMT)

w

Day of week (from 0—Sunday, to 6—Saturday)

W

Week number (according to ISO 8601, from 1 to 53)

y

Year (two digits)

Y

Year (four digits)

z

Day of year (from 0 to 365)

Z

Time zone difference to UTC (in seconds)


The function date() is very powerful and offers a broad range of ways to use it. However, especially if you have localized content, you need some good phrases. In this chapter, you will find many of them.

PHP's date and time functions have their own section in the PHP manual. You can find more information about date() and friends at http://php.net/datetime.

Using Text Within date()

date('\To\d\a\y \i\s \t\he jS \d\a\y of F')

Imagine you want to output the current date (or a specific date) and use custom strings in it. The code could look like a mess if you are trying it like this:

<?php
 echo 'Today is the ' . date('jS') . ' day of the 
  month ' . date('F');
?>
<?php
 echo date('\To\d\a\y \i\s \t\he jS \d\a\y of F');
?>

Using Ordinary Characters in date() (date.php)

The output of this script is something like this, depending on the current date:

Today is the 3rd day of the month May

The behavior of date() is the following: All characters within the first parameter that bear a special meaning (for example, formatting symbols) get replaced by the appropriate values. All other characters, however, remain unchanged. If a character is escaped using the backslash character (\), it is returned verbatim. So, the code at the beginning of this phrase shows a new version of the code that only uses one call to date().

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

 Big Nerd RanchAsk Big Nerd Ranch: Blocks in Objective-C
By Big Nerd Ranch on June 24, 2010 No Comments

Adam Preble answers a question about blocks.

Danny KalevYves Smith: Suspicions that The Fed is manipulating Wall Street
By Danny Kalev on May 24, 2010 No Comments

Yves Smith, the nom de plume of the creator of Naked Capitalism and one of the most savvy and respected members of the blogosphere. In professional life Yves is known as Susan Webber. Yves recently gave an interview to an Israeli financial newspaper in which she claims that a federal team unofficially called "the plunge protection team" is manipulating the stocks on Wall Street.

 Big Nerd RanchAsk Big Nerd Ranch: Rotating an iPhone View Around a Point
By Big Nerd Ranch on May 20, 2010 No Comments

Brian Hardy answers a question about view rotation.

Sample code for this article is available in the Big Nerd Ranch github repository. The sample application demonstrates several techniques illustrated here, and works on iPhone or iPad.

Q. On iPhone OS, how can I rotate a view around an arbitrary point?

A. By default, views in Cocoa Touch (and Cocoa) are configured to rotate around their center point. While this is commonly useful (think of a UIActivityIndicatorView), often you will want to use a point other than the center. There are (at least) two ways of doing this. You can change the anchorPoint property of the view's layer. Alternatively, you can wrap the view in a superview, with the superview's center located at the point you want to rotate around. In either case, the mechanism for rotation is the same. Both techniques are discussed here.

See All Related Blogs

Informit Network