Page 144 - Open Soource Technologies 304.indd
P. 144
Unit 8: Working with Strings, Date and Time
Notes
2004W021 2004-01-04 23:00:00 Midnight of the first day of ISO
week 21 in 2004.
2004122 0915 2004-12-22 08:15:00 Only numbers in the form yyyy
mmdd hhmm.
Using the strtotime() function is easy. It accepts two parameters: the string to parse to a timestamp
and an optional timestamp. If the timestamp is included, the time is converted relative to the
timestamp; if it’s not included, the current time is used. The relative calculations are only written
with yesterday, tomorrow, and the 1 year 2 days (ago) format strings. strtotime() parsing is
always done with the current time zone, unless a different time zone is specified in the string
that is parsed:
<?php
echo date(“H:i T\n”, strtotime(“09:22”)); // shows 09:22 CET
echo date(“H:i T\n\n”, strtotime(“09:22 GMT”)); // shows 10:22 CET
echo gmdate(“H:i T\n”, strtotime(“09:22”)); // shows 08:22 GMT
echo gmdate(“H:i T\n”, strtotime(“09:22 GMT”)); // shows 09:22 GMT
?>
8.5 Strings with PHP
Strings in PHP are a sequence of characters that are always internally nullterminated. However,
unlike some other languages, such as C, PHP does not rely on the terminating null to calculate
a string’s length, but remembers its length internally. This allows for easy handling of binary
data in PHP—for example, creating an image on-the-fly and outputting it to the browser. The
maximum length of strings varies according to the platform and C compiler, but you can expect
it to support at least 2GB. Don’t write programs that test this limit because you’re likely to first
reach your memory limit. When writing string values in your source code, you can use double
quotes (“), single quotes (‘) or here-docs to delimit them. Each method is explained in this section.
Double Quotes Examples for double quotes:
“PHP: Hypertext Pre-processor”
“GET / HTTP/1.0\n”
“1234567890”
Strings can contain pretty much all characters. Some characters can’t be written as is, however,
and require special notation:
\n Newline.
\t Tab.
\” Double quote.
\\ Backslash.
\0 ASCII 0 (null). Contd...
LOVELY PROFESSIONAL UNIVERSITY 139