Getting the Current Date
Syntax
date('format options');
Example:
1.date('y m d'); Output: 05 04 23
2. date('Y M D'); Output: 2005 Apr Sat
3.date('Y m d h: s: m'); Output: 2005 04 23 02: 18: 04 (Date and Time)
Number of days in a month
cal_days_in_month( CAL_GREGORIAN, $intMonth, $intYear);
Example:
cal_days_in_month ( CAL_GREGORIAN, 1, 2005); Output: 31
Check the date format
checkdate ($intMonth,$date, $intYear);
Example:
1. checkdate (1, 30,2005); Output: (1 means true)
2. checkdate (2, 30,2005); Output: (null means false)
Welcome! PHP has nice built in date function which allows you to display dates in human readable formats.
CODE:
echo date("Y-m-d");
echo date("Y/m/d");
echo date("M d, Y");
echo date("F d, Y");
echo date("D M d, Y");
echo date("l F d, Y");
echo date("l F d, Y, h:i:s");
echo date("l F d, Y, h:i A");
?>
2008-05-21
2008/05/21
May 21, 2008
May 21, 2008
Wed May 21, 2008
Wednesday May 21, 2008
Wednesday May 21, 2008, 04:42:26
Wednesday May 21, 2008, 04:42 AM
PHP Date Function Parameters
ormat - The first parameter, format, in the date function specifies how to display the date/time. It uses different letters to represent the date and time. Some of the letters used above are described here.- d - The day of the month, i.e. 01-31
- m - Month representation in numbers, i.e. 01-12
- Y - Year in four digits
PHP date function explained
With PHP's date function you format timestamps, so they are more human readable.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.
PHP Date - What Time Is It?
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.
date("m/d/y"); ?>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!
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:
$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")
- 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
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.
timestamp - The second parameter, timestamp, is an optional parameter. Timestamp is the number of seconds since January 1, 1970 at 00:00:00 GMT. This is also known as the Unix Timestamp.
PHP Date: Finding Date / Time
Using the 2nd timestamp parameter we can do things like say find exactly what date or day it was yesterday or a week ago or what date it will be 1 month from today.
There are two ways we can do that.
- Using the PHP strtotime function.
- Using the PHP mktime function.
- strtotime - Convert any English textual datetime description into a Unix timestamp.
- mktime - Get Unix timestamp for a date.
PHP Date: Using strtotime to find date/time
Let's see some of the examples to find out dates using date and strtotime function.
Find Yesterday’s date
echo "yesterday was ".date("Y-m-d", strtotime("-1 days"));
?>
Output
yesterday was 2008-05-20
Find Date one week ago
echo "1 week form today was ".date("Y-m-d", strtotime("-1 weeks"));
?>
Output
1 week form today was 2008-05-14
Find Date one month after
echo "1 month from today will be ".date("Y-m-d", strtotime("+1 months"));
?>
Output
1 month form today will be 2008-06-21
PHP Date: Using mktime to find date/time
mktime could be used to find more specific things like find the next leap year in the calendar.
Find Leap Year
$day = "";
/*
* since leap year falls ever 4 years so loop for 4 times
*/
for($i=0; $i<4; $i++)
{
//get day timestamp for feburary 29 for this year
$day = date("d", mktime(0, 0, 0, 2, 29, date("Y")+$i));
/*
* check if day equals 29.
* If day is 29 then it must be the leap year. if day is 01, then it not a leap year.
*/
if($day == 29)
{
$year = date("Y")+$i;
break;
}
}
echo "next leap year is in year $year";
?>
Output
next leap year is in year 2008
The mktime takes 6 arguments. The parameters are explained as below.
- hour - The number of the hour.
- minute - The number of the minute.
- second - The number of seconds past the minute.
- month - The number of the month.
- day - The number of the day.
- year - The number of year.
No comments:
Post a Comment