Core Web Application Development with PHP and MySQL
To understand how to work with dates and times in PHP, you must know how such are represented in software. This section covers three primary sources of date/time values. PHP
The time function represents the most common way to obtain a date/time value. This method returns a timestamp for the current date and time and can be passed to most other date/time functions in PHP. <?php // get current time $now = time(); // print that time. The date() function is explained soon. echo date('r', $now); ?> Depending on the actual date and time, the preceding script would output something like this: Sun, 18 Sep 2005 14:12:55 -0800
You can use a number of other functions to obtain the current date and time in array format, such as the getdate function (examined in more detail in the section "Getting the Date and Time"). The Operating System
Sometimes the operating system returns a date/time value, as with the filectime, filemtime, and fileatime functions (see Chapter 24, "Files and Directories"). These functions also return timestamps that can be passed directly to the date function, as follows: <?php $mtime = filemtime('/usr/local/lib/php.ini'); echo <<<EOM The contents of <em>php.ini</em> were last modified on:<br/> EOM; date('r', $mtime); ?> The preceding code outputs something like this: The contents of php.ini were last modified on: Mon, 03 Jan 2005 09:34:59 -0800
The Database Server
Database servers represent another major source of date/time values. Nearly all of these have a wide variety of types to store dates, times, dates and times, or time intervals. These values enable you to store all sorts of data in database tables, such as account creation times, last access times, and transaction execution times. Typically, database servers return these values in string format; a format of '2005-01-01 12:00:01AM' is common for date/time combinations, and '2005-01-01' is typical for simple dates. An advantage of this formatting is the capability to send these strings directly to the client browser, such as in the following example: <?php $results = $conn->query('SELECT name, birthdate FROM users'); if ($results !== FALSE) { echo "<table width='100%' border='0'\n"; echo "<tr>\n"; echo "<td width='30%'>Name:</td><td>Birthdate:</td>\n"; while (($row = $results->fetch_assoc()) !== NULL) { echo "<td>$row['name']</td><td>$row['birthdate']</td>\n"; } echo "</tr>\n"; echo "</table>\n"; $results->close(); } ?>
When we're using database server functionality for dates and times, we do not have to worry about processing these types in PHP scripts. Web Pages and Users
Client forms presented to customers are another source of date/time values. If a form requires customers to disclose their birthday, for example, or to disclose when they first joined an organization, the form returns those values (in the $_POST superglobal array). You can save yourself some work by planning ahead how users will enter these values. If you give the user a simple <input> field into which to put a date, such as the following Birthdate: <input type='text' name='birthdate' size='10'/>
nothing prevents the user from providing any of the following values: 1980-02-15 02/02/80 02.05-79 121975 yesterday abcdefghig
Parsing all of these to determine correctness might prove annoying, at best, and brutally frustrating at worst. You can avoid this by prompting users as to the date format that you want with an initial value for the text box, such as this: Birthdate: <input type='text' name='birthdate' value='yyyy-mm-dd' size='10'/>
In this case, users probably will not be surprised if a server rejects the date 09/10/1975. However, a much better solution is just to split the different components of the date (year, month, and day) into their own input fields, as follows: Birthdate: <input type='text' name='year' value='yyyy' size='4'/> <input type='text' name='month' value='mm' size='2'/> <input type='text' name='day' value='dd' size='2'/>
Users will see something in their browser similar to what Figure 26-1 shows, which means less user confusion and less stress in our PHP scripts in terms of determining a valid date. Figure 26-1. Entering dates as individual components.
Chapter 20, "User Authentication," demonstrated a solution for an account creation form: using drop-down boxes (via <option> markup elements in HTML) to provide a fixed set of values from which the user must select. |
Категории