php - string explode,php implode, timestamp


php - string explode
The PHP function explode lets you take a string and blow it up into smaller pieces. For example, if you had a sentence you could ask explode to use the sentence's spaces " " as dynamite and it would blow up the sentence into separate words, which would be stored in an array. The sentence "Hello, I would like to lose weight." would look like this after explode got done with it:
1.     Hello,
2.     I
3.     would
4.     like
5.     to
6.     lose
7.     weight.
The dynamite (the space character) disappears, but the other stuff remains, but in pieces. With that abstract picture of the explode function in mind, lets take a look at how it really works.

he explode function

The first argument that explode takes is the delimiter (our dynamite) which is used to blow up the second argument, the original string. explode returns an array of string pieces from the original and they are numbered in order, starting from 0. Lets take a phone number in the form ###-###-#### and use a hyphen "-" as our dynamite to split the string into three separate chunks.

PHP Code:

$rawPhoneNumber = "800-555-5555";


$phoneChunks = explode("-", $rawPhoneNumber);

echo "Raw Phone Number = $rawPhoneNumber <br />";

echo "First chunk = $phoneChunks[0]<br />";

echo "Second chunk = $phoneChunks[1]<br />";

echo "Third Chunk chunk = $phoneChunks[2]";

 

explode function - setting a limit
If you want to control the amount of destruction that explode can wreak on your original string, consider using the third (optional) argument which allows you to set the number of pieces explode can return. This means it will stop exploding once the number of pieces equals the set limit. Below we've blown up a sentence with no limit and then with a limit of 4.

 

PHP Code:

$someWords = "Please don't blow me to pieces.";

$wordChunks = explode(" ", $someWords);
for($i = 0; $i < count($wordChunks); $i++){
                echo "Piece $i = $wordChunks[$i] <br />";
}

$wordChunksLimited = explode(" ", $someWords, 4);
for($i = 0; $i < count($wordChunksLimited); $i++){
                echo "Limited Piece $i = $wordChunksLimited[$i] <br />";
}

php - array implode
The PHP function implode operates on an array and is known as the "undo" function of explode. If you have used explode to break up a string into chunks or just have an array of stuff you can use implode to put them all into one string.

php implode - repairing the damage

The first argument of implode is the string of characters you want to use to join the array pieces together. The second argument is the array (pieces).

PHP Code:

$pieces = array("Hello", "World,", "I", "am", "Here!");

$gluedTogetherSpaces = implode(" ", $pieces);
$gluedTogetherDashes = implode("-", $pieces);
for($i = 0; $i < count($pieces); $i++){
                echo "Piece #$i = $pieces[$i] <br />";
}
echo "Glued with Spaces = $gluedTogetherSpaces <br />";
echo "Glued with Dashes = $gluedTogetherDashes";

php date function
While PHP's date() function may seem to have an overwhelming amount of options available, isn't it always better to have more choices than not enough? With PHP's date function you format timestamps, so they are more human readable.
This lesson will teach you how to display the current time, formating PHP's timestamp, and show you all the various date arguments for reference purposes.
php date - the timestamp
The date function always formats a timestamp, whether you supply one or not. What's a timestamp? Good question!
  • Timestamp: A timestamp is the number of seconds from January 1, 1970 at 00:00. Otherwise known as the Unix Timestamp, this measurement is a widely used standard that PHP has chosen to utilize.
The date function uses letters of the alphabet to represent various parts of a typical date and time format. The letters we will be using in our first example are:
  • d: The day of the month. The type of output you can expect is 01 through 31.
  • m: The current month, as a number. You can expect 01 through 12.
  • y: The current year in two digits ##. You can expect 00 through 99
We'll tell you the rest of the options later, but for now let's use those above letters to format a simple date! The letters that PHP uses to represent parts of date and time will automatically be converted by PHP.
However, other characters like a slash "/" can be inserted between the letters to add additional formatting. We have opted to use the slash in our example.

PHP Code:

<?php
echo date("m/d/y");
?>

Be sure to test this out on your own PHP enabled server, it's really great to see the instant results available with PHP date!

supplying a timestamp

As our first example shows, the first argument of the date function tells PHP how you would like your date and time displayed. The second argument allows for a timestamp and is optional.
This example uses the mktime function to create a timestamp for tomorrow. To go one day in the future we simply add one to the day argument of mktime. For your future reference, we have the arguments of mktime.
Note: These arguments are all optional. If you do not supply any arguments the current time will be used to create the timestamp.
  • mktime(hour, minute, second, month, day, year, daylight savings time)

PHP Code:

<?php
$tomorrow = mktime(0, 0, 0, date("m"), date("d")+1, date("y"));
echo "Tomorrow is ".date("m/d/y", $tomorrow);
?>

Notice that we used one letter at a time with the function date to get the month, day and year. For example the date("m") will return the month's number 01-12.
If we were to run our new script just after the 2010 Winter Olympics our display would look like:
php date - reference
Now that you know the basics of using PHP's date function, you can easily plug in any of the following letters to format your timestamp to meet your needs.
Important Full Date and Time:
  • r: Displays the full date, time and timezone offset. It is equivalent to manually entering date("D, d M Y H:i:s O")
Time:
  • a: am or pm depending on the time
  • A: AM or PM depending on the time
  • g: Hour without leading zeroes. Values are 1 through 12.
  • G: Hour in 24-hour format without leading zeroes. Values are 0 through 23.
  • h: Hour with leading zeroes. Values 01 through 12.
  • H: Hour in 24-hour format with leading zeroes. Values 00 through 23.
  • i: Minute with leading zeroes. Values 00 through 59.
  • s: Seconds with leading zeroes. Values 00 through 59.
Day:
  • d: Day of the month with leading zeroes. Values are 01 through 31.
  • j: Day of the month without leading zeroes. Values 1 through 31
  • D: Day of the week abbreviations. Sun through Sat
  • l: Day of the week. Values Sunday through Saturday
  • w: Day of the week without leading zeroes. Values 0 through 6.
  • z: Day of the year without leading zeroes. Values 0 through 365.
Month:
  • m: Month number with leading zeroes. Values 01 through 12
  • n: Month number without leading zeroes. Values 1 through 12
  • M: Abbreviation for the month. Values Jan through Dec
  • F: Normal month representation. Values January through December.
  • t: The number of days in the month. Values 28 through 31.
Year:
  • L: 1 if it's a leap year and 0 if it isn't.
  • Y: A four digit year format
  • y: A two digit year format. Values 00 through 99.
Other Formatting:
  • U: The number of seconds since the Unix Epoch (January 1, 1970)
  • O: This represents the Timezone offset, which is the difference from Greenwich Meridian Time (GMT). 100 = 1 hour, -600 = -6 hours
We suggest that you take a few minutes to create several timestamps using PHP's mktime function and just try out all these different letters to get your feet wet with PHP's date function

Comments