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.
The date function always formats a timestamp, whether you supply one or not. What's a timestamp? Good question!
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:
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.
If the 2010 Winter Olympics were just finishing up, you would see something like:
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!
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.
$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:
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:
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.
© 2018 BreakInerview