Handling dates is an important part of many applications, and fortunately, it is relatively easy to work with dates in PHP.
Set the Timezone
Before jumping into how to handle dates, it is worth noting that PHP by default will use the date and time from the server. If the time zone of your server differs from your timezone, or the people using your website, you can have some unexpected results. To determine what your time zone is, you can use:
<?php
echo date_default_timezone_get(); // America/Los_Angeles for Western
?>There are a couple of options available if you want to change the time zone. If you want to change the time zone in just your current PHP script, find your time zone, and then use:
<?php
date_default_timezone_set('America/Los_Angeles');
?>If you would like to change it for all PHP scripts on the server, you can edit the php configuration file (php.ini) :
Get the Current Date
Now that we have your time zone set correctly, let's look at the simple use case of echo'ing out the current date. To do this, you can use the date function.
<?php
echo date('m/d/y'); // 02/01/11
?>It's easy to apply a different format to the date, just replace the characters in the above example with any of the following:
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")
- 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.
- 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.
- 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.
- 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.
- 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
Timestamps and Dates
Echo'ing the current date is easy, but how about calculating dates in the future or past? In the above examples, we simply provided the formatting as the first parameter and the date function spit out the current date/time. If you want to work with dates other than the current one, you will need to use the second parameter of the date function, which is a timestamp.
A timestamp is UNIX speak for the number of seconds that have passed since January 1, 1970. To generate the current date and time via a timestamp, you can use the mktime function. When you call the date function, it is grabbing the current timestamp by default and you don't need to provide the timestamp:
<?php
echo date('m/d/y');
echo date('m/d/y', mktime());
?>Manipulating Dates
Manipulating dates is hard until you get the basics of timestamps. There are a couple of ways to generate dates in the part or future. One way is to simply add or subtract the number of seconds for a given duration. For example, there are 86,400 seconds in a day, so if we wanted to display the date for tomorrow, you could simply use the below code to generate the current timestamp, then add 86,400 seconds (a day) to the timestamp, which gives us the timestamp for tomorrow:
<?php
echo date('m/d/y', (mktime() + 86400));
?>Calculating the number of seconds in a month or year can be a bit laborious, so let's take a look at some other functions to help with manipulating and calculating dates. First up is the strtotime function, which expects to be given a string containing an English date format and will return the timestamp for the given string. To get the date for tomorrow, and next month, and next year, we could use:
<?php
echo date('m/d/y', strtotime("+1 day")) . '<br />';
echo date('m/d/y', strtotime("+1 month")). '<br />';
echo date('m/d/y', strtotime("+1 year")). '<br />';
?>The strtotime function can also come in handy if you are dealing with a date column from MySQL (e.g. it is formatted as YYYY-MM-DD):
<?php
$mysqlDate = '2011-01-20';
echo date('F j, Y', strtotime($mysqlDate));
?>Validating Dates
I can't believe you've made it this far... Since you have, let's look at validating a date that could come from a user. As any good coder knows, we never trust input that comes from 3rd parties, whether that is an end user entering a value into an HTML form, or data coming from a web service.
With that in mind, how do we validate that an end user entered in a correct date into a form field? If we use the strtotime function with an invalid date, it will return false. So it strtotime can’t parse the date, we know it is not a valid date!
<?php
if(!strtotime('2011-13-20')) {
echo 'That is a bad date';
}
?>In Conclusion
Hopefully, you know a bit more about how to work with dates in PHP. We've only scratched the surface, and there are a lot of other functions available, so keep on learning!