PHP CookBook Free Open Book

PHP CookBook

Previous Section Next Section

Recipe 3.2 Finding the Current Date and Time

3.2.1 Problem

You want to know what the time or date is.

3.2.2 Solution

Use strftime( ) or date( ) for a formatted time string:

print strftime('%c');
print date('r');
Mon Aug 12 18:23:45 2002
Mon, 12 Aug 2002 18:23:45 -0400

Use getdate( ) or localtime( ) if you want time parts:

$now_1 = getdate( );
$now_2 = localtime( );
print "$now_1[hours]:$now_1[minutes]:$now_1[seconds]";
print "$now_2[2]:$now_2[1]:$now_2[0]";
18:23:45
18:23:45

3.2.3 Discussion

The functions strftime( ) and date( ) can produce a variety of formatted time and date strings. They are discussed in more detail in Recipe 3.5. Both localtime( ) and getdate( ), on the other hand, return arrays whose elements are the different pieces of the specified date and time.

The associative array getdate( ) returns has the key/value pairs listed in Table 3-1.

Table 3-1. Return array from getdate( )

Key

Value

seconds

Seconds

minutes

Minutes

hours

Hours

mday

Day of the month

wday

Day of the week, numeric (Sunday is 0, Saturday is 6)

mon

Month, numeric

year

Year, numeric

yday

Day of the year, numeric (e.g., 299)

weekday

Day of the week, textual, full (e.g., "Friday")

month

Month, textual, full (e.g., "January")

For example, here's how to use getdate( ) to print out the month, day, and year:

$a = getdate( );
printf('%s %d, %d',$a['month'],$a['mday'],$a['year']);
August 7, 2002

Pass getdate( ) an epoch timestamp as an argument to make the returned array the appropriate values for local time at that timestamp. For example, the month, day, and year at epoch timestamp 163727100 is:

$a = getdate(163727100);
printf('%s %d, %d',$a['month'],$a['mday'],$a['year']);
March 10, 1975

The function localtime( ) returns an array of time and date parts. It also takes an epoch timestamp as an optional first argument, as well as a boolean as an optional second argument. If that second argument is true, localtime( ) returns an associative array instead of a numerically indexed array. The keys of that array are the same as the members of the tm_struct structure that the C function localtime( ) returns, as shown in Table 3-2.

Table 3-2. Return array from localtime( )

Numeric position

Key

Value

0

tm_sec

Second

1

tm_min

Minutes

2

tm_hour

Hour

3

tm_mday

Day of the month

4

tm_mon

Month of the year (January is 0)

5

tm_year

Years since 1900

6

tm_wday

Day of the week

7

tm_yday

Day of the year

8

tm_isdst

Is daylight saving time in effect?

For example, here's how to use localtime( ) to print out today's date in month/day/year format:

$a = localtime( );
$a[4] += 1;
$a[5] += 1900;
print "$a[4]/$a[3]/$a[5]";
8/7/2002

The month is incremented by 1 before printing since localtime( ) starts counting months with 0 for January, but we want to display 1 if the current month is January. Similarly, the year is incremented by 1900 because localtime( ) starts counting years with 0 for 1900.

Like getdate( ), localtime( ) accepts an epoch timestamp as an optional first argument to produce time parts for that timestamp:

$a = localtime(163727100);
$a[4] += 1;
$a[5] += 1900;
print "$a[4]/$a[3]/$a[5]";
3/10/1975

3.2.4 See Also

Documentation on strftime( ) at http://www.php.net/strftime, date( ) at http://www.php.net/date, getdate( ) at http://www.php.net/getdate, and localtime( ) at http://www.php.net/localtime.

    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Preface
    Chapter 1. Strings
    Chapter 2. Numbers
    Chapter 3. Dates and Times
    3.1 Introduction
    Recipe 3.2 Finding the Current Date and Time
    Recipe 3.3 Converting Time and Date Parts to an Epoch Timestamp
    Recipe 3.4 Converting an Epoch Timestamp to Time and Date Parts
    Recipe 3.5 Printing a Date or Time in a Specified Format
    Recipe 3.6 Finding the Difference of Two Dates
    Recipe 3.7 Finding the Difference of Two Dates with Julian Days
    Recipe 3.8 Finding the Day in a Week, Month, Year, or the Week Number in a Year
    Recipe 3.9 Validating a Date
    Recipe 3.10 Parsing Dates and Times from Strings
    Recipe 3.11 Adding to or Subtracting from a Date
    Recipe 3.12 Calculating Time with Time Zones
    Recipe 3.13 Accounting for Daylight Saving Time
    Recipe 3.14 Generating a High-Precision Time
    Recipe 3.15 Generating Time Ranges
    Recipe 3.16 Using Non-Gregorian Calendars
    Recipe 3.17 Program: Calendar
    Chapter 4. Arrays
    Chapter 5. Variables
    Chapter 6. Functions
    Chapter 7. Classes and Objects
    Chapter 8. Web Basics
    Chapter 9. Forms
    Chapter 10. Database Access
    Chapter 11. Web Automation
    Chapter 12. XML
    Chapter 13. Regular Expressions
    Chapter 14. Encryption and Security
    Chapter 15. Graphics
    Chapter 16. Internationalization and Localization
    Chapter 17. Internet Services
    Chapter 18. Files
    Chapter 19. Directories
    Chapter 20. Client-Side PHP
    Chapter 21. PEAR
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele