1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP and MySQL Web Development - P82 potx

5 233 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

377 Next Next We’ll move on to Chapter 18,“Managing the Date and Time,” and look at PHP’s libraries of date and calendar functions.You’ll see how to convert from user-entered for- mats to PHP formats to MySQL formats, and back again. 22 525x ch17 1/24/03 3:40 PM Page 377 22 525x ch17 1/24/03 3:40 PM Page 378 18 Managing the Date and Time IN THIS CHAPTER,WE’LL DISCUSS CHECKING and formatting the date and time and con- verting between date formats.This is especially important when converting between MySQL and PHP date formats, Unix and PHP date formats, and dates entered by the user in an HTML form. We’ll cover n Getting the date and time in PHP n Converting between PHP and MySQL date formats n Calculating dates n Using the calendar functions Getting the Date and Time from PHP Way back in Chapter 1,“PHP Crash Course,” we talked about using the date() func- tion to get and format the date and time from PHP.We’ll talk about it and some of PHP’s other date and time functions in a little more detail now. Using the date() Function As you might recall, the date() function takes two parameters, one of them optional. The first one is a format string, and the second, optional one is a UNIX time stamp. If you don’t specify a time stamp, then date() will default to the current date and time. It returns a formatted string representing the appropriate date. A typical call to the date function could be echo date('jS F Y'); This will produce a date of the format “27 th August 2000”. The format codes accepted by date are listed in Table 18.1. 23 525x ch18 1/27/03 2:53 PM Page 379 380 Chapter 18 Managing the Date and Time Table 18.1 Format Codes for PHP’s date() Function Code Description a Morning or afternoon, represented as two lowercase characters, either “am” or “pm”. A Morning or afternoon, represented as two uppercase characters, either “AM” or “PM”. BSwatch Internet time, a universal time scheme. More information is available at http://www.swatch.com/. dDay of the month as a 2-digit number with a leading zero. Range is from “01” to “31”. DDay of the week in 3-character abbreviated text format. Range is from “Mon” to “Sun”. F Month of the year in full text format. Range is from “January” to “December”. g Hour of the day in 12-hour format without leading zeroes. Range is from “1” to “12”. G Hour of the day in 24-hour format without leading zeroes. Range is from “0” to “23”. h Hour of the day in 12-hour format with leading zeroes. Range is from “01” to “12”. H Hour of the day in 24-hour format with leading zeroes. Range is from “00” to “23”. i Minutes past the hour with leading zeroes. Range is from “00” to “59”. IDaylight savings time, represented as a Boolean value.This will return “1” if the date is in daylight savings and “0” if it is not. jDay of the month as a number without leading zeroes. Range is from “1” to “31”. lDay of the week in full text format. Range is from “Monday” to “Sunday”. L Leap year, represented as a Boolean value.This will return “1” if the date is in a leap year and “0” if it is not. m Month of the year as a 2-digit number with leading zeroes. Range is from “01” to “12”. M Month of the year in 3-character abbreviated text format. Range is from “Jan” to “Dec”. n Month of the year as a number without leading zeroes. Range is from “1” to “12”. O Difference between the current timezone and Greenwich Mean Time in hours e.g. +1600. r RFC822 formatted date and time, for example Wed, 9 Oct 2002 18:45:30 +1600. (Added in PHP 4.0.4.) s Seconds past the minute with leading zeroes. Range is from “00” to “59”. SOrdinal suffix for dates in 2-character format.This can be “st”,“nd”, “rd”, or “th”, depending on the number it is after. tTotal number of days in the date’s month. Range is from “28” to “31”. T Timezone setting of the server in 3-character format, for example,“EST”. UTotal number of seconds from 1 January 1970 to this time; a.k.a., a UNIX time stamp for this date. wDay of the week as a single digit. Range is from “0” (Sunday) to “6” (Saturday). WWeek number in the year, ISO-8601 compliant. (Added at PHP 4.1.0.) 23 525x ch18 1/27/03 2:53 PM Page 380 381 Getting the Date and Time from PHP yYear in 2-digit format, for example,“00”. YYear in 4-digit format, for example,“2000”. zDay of the year as a number. Range is “0” to “365”. Z Offset for the current timezone in seconds. Range is “-43200” to “43200”. Dealing with Unix Timestamps The second parameter to the date() function is a Unix time stamp. In case you are wondering exactly what this means, most Unix systems store the cur- rent time and date as a 32-bit integer containing the number of seconds since midnight, January 1, 1970, GMT, also known as the Unix Epoch.This can seem a bit esoteric if you are not familiar with it, but it’s a standard. Unix timestamps are a compact way of storing a date and time, but it is worth noting that they do not suffer from the year 2000 (Y2K) problem that affects some other com- pact or abbreviated date formats. If your software is still in use in 2038, there will be similar problems though. As timestamps do not have a fixed size, but are tied to the size of a C long, which is at least 32 bits, the most likely solution is that by 2038, your com- piler will use a larger type. Even if you are running PHP on a Windows server, this is still the format that is used by date() and a number of other PHP functions. If you want to convert a date and time to a Unix time stamp, you can use the mktime() function.This has the following prototype: int mktime (int hour, int minute, int second, int month, int day, int year [, int is_dst]) The parameters are fairly self-explanatory, with the exception of the last one, is_dst, which represents whether the date was in daylight savings time or not.You can set this to 1 if it was, 0 if it wasn’t, or -1 (the default value) if you don’t know.This is optional so you will rarely use it anyway. The main trap to avoid with this function is that the parameters are in a fairly unin- tuitive order.The ordering doesn’t lend itself to leaving out the time. If you are not wor- ried about the time, you can pass in 0s to the hour, minute, and second parameters.You can, however, leave out values from the right side of the parameter list. If you leave the parameters blank, they will be set to the current values. Hence a call such as $timestamp = mktime(); will return the Unix timestamp for the current date and time.You could, of course, also get this by calling $timestamp = date("U"); Table 18.1 Continued Code Description 23 525x ch18 1/27/03 2:53 PM Page 381 . 18,“Managing the Date and Time,” and look at PHP s libraries of date and calendar functions.You’ll see how to convert from user-entered for- mats to PHP formats to MySQL formats, and back again. 22. Date and Time from PHP Way back in Chapter 1, PHP Crash Course,” we talked about using the date() func- tion to get and format the date and time from PHP. We’ll talk about it and some of PHP s. PHP date formats, Unix and PHP date formats, and dates entered by the user in an HTML form. We’ll cover n Getting the date and time in PHP n Converting between PHP and MySQL date formats n Calculating

Ngày đăng: 07/07/2014, 03:20

Xem thêm: PHP and MySQL Web Development - P82 potx

Mục lục

    PHP and MySQL Web Development

    Part I: Using PHP

    Chapter 1: PHP Crash Course

    Chapter 2: Storing and Retrieving Data

    Chapter 4: String Manipulation and Regular Expressions

    Chapter 5: Reusing Code and Writing Functions

    Part II: Using MySQL

    Chapter 7: Designing Your Web Database

    Chapter 8: Creating Your Web Database

    Chapter 9: Working with Your MySQL Database

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN