- PHP Date and Time
- The PHP Date() Function
- Syntax
- Get a Date
- Example
- PHP Tip — Automatic Copyright Year
- Example
- Get a Time
- Example
- Get Your Time Zone
- Example
- Create a Date With mktime()
- Syntax
- Example
- Create a Date From a String With strtotime()
- Syntax
- Example
- Example
- More Date Examples
- Example
- Example
- Complete PHP Date Reference
- Get the Current Month of a Date in PHP
- Using the date() Function to Get the Current Month of a Date in PHP
- Using strtotime() and date() Functions to Get the Current Month of a Date in PHP
- Get the Current Month by Using the DateTime Class in PHP
- Related Article — PHP DateTime
- How to Get Month From a Date in PHP?
- Problem:
- Solution:
- Method 1: Using date() function to retrieve current month
- Method 2: Using strtotime() and date() function to retrieve month from any date
- Method 3: Using DateTime class to get current month
- Method 4: Using CreateFromFormat() method to get month from any date
- Courses
PHP Date and Time
The PHP date() function is used to format a date and/or a time.
The PHP Date() Function
The PHP date() function formats a timestamp to a more readable date and time.
Syntax
Parameter | Description |
---|---|
format | Required. Specifies the format of the timestamp |
timestamp | Optional. Specifies a timestamp. Default is the current date and time |
A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.
Get a Date
The required format parameter of the date() function specifies how to format the date (or time).
Here are some characters that are commonly used for dates:
- d — Represents the day of the month (01 to 31)
- m — Represents a month (01 to 12)
- Y — Represents a year (in four digits)
- l (lowercase ‘L’) — Represents the day of the week
Other characters, like»/», «.», or «-» can also be inserted between the characters to add additional formatting.
The example below formats today’s date in three different ways:
Example
echo «Today is » . date(«Y/m/d») . «
«;
echo «Today is » . date(«Y.m.d») . «
«;
echo «Today is » . date(«Y-m-d») . «
«;
echo «Today is » . date(«l»);
?>?php
PHP Tip — Automatic Copyright Year
Use the date() function to automatically update the copyright year on your website:
Example
Get a Time
Here are some characters that are commonly used for times:
- H — 24-hour format of an hour (00 to 23)
- h — 12-hour format of an hour with leading zeros (01 to 12)
- i — Minutes with leading zeros (00 to 59)
- s — Seconds with leading zeros (00 to 59)
- a — Lowercase Ante meridiem and Post meridiem (am or pm)
The example below outputs the current time in the specified format:
Example
Note that the PHP date() function will return the current date/time of the server!
Get Your Time Zone
If the time you got back from the code is not correct, it’s probably because your server is in another country or set up for a different timezone.
So, if you need the time to be correct according to a specific location, you can set the timezone you want to use.
The example below sets the timezone to «America/New_York», then outputs the current time in the specified format:
Example
Create a Date With mktime()
The optional timestamp parameter in the date() function specifies a timestamp. If omitted, the current date and time will be used (as in the examples above).
The PHP mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Syntax
The example below creates a date and time with the date() function from a number of parameters in the mktime() function:
Example
Create a Date From a String With strtotime()
The PHP strtotime() function is used to convert a human readable date string into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).
Syntax
The example below creates a date and time from the strtotime() function:
Example
PHP is quite clever about converting a string to a date, so you can put in various values:
Example
$d=strtotime(«next Saturday»);
echo date(«Y-m-d h:i:sa», $d) . «
«;
However, strtotime() is not perfect, so remember to check the strings you put in there.
More Date Examples
The example below outputs the dates for the next six Saturdays:
Example
$startdate = strtotime(«Saturday»);
$enddate = strtotime(«+6 weeks», $startdate);
?php
while ($startdate < $enddate) echo date("M d", $startdate) . "
«;
$startdate = strtotime(«+1 week», $startdate);
>
?>
The example below outputs the number of days until 4th of July:
Example
$d1=strtotime(«July 04»);
$d2=ceil(($d1-time())/60/60/24);
echo «There are » . $d2 .» days until 4th of July.»;
?>?php
Complete PHP Date Reference
For a complete reference of all date functions, go to our complete PHP Date Reference.
The reference contains a brief description, and examples of use, for each function!
Get the Current Month of a Date in PHP
- Using the date() Function to Get the Current Month of a Date in PHP
- Using strtotime() and date() Functions to Get the Current Month of a Date in PHP
- Get the Current Month by Using the DateTime Class in PHP
The date() function is a built-in PHP function that formats the timestamp. In UNIX Timestamp, the computer saves the date and time. Since January 1, 1970, this time has been measured in seconds. Because this is difficult for humans to comprehend, PHP changes the timestamp to a more legible and intelligible format.
There are several methods to get the month portion of a date in PHP. In the following sections, you’ll learn how to get the month of date from the current date or any date.
Using the date() Function to Get the Current Month of a Date in PHP
PHP’s date() function can provide you with the date and time-related information based on the formatting characters in its first parameter. A maximum of two arguments can be sent to the function. It will return information about the current time if you just use one argument.
To generate three distinct forms of a month, use three different formatting characters in the first parameter of the date() function. These are the formatting characters:
The date() function has the following formatting options: The date() function’s format parameter is a string that can include several characters, allowing you to produce dates in a variety of forms, as seen below:
php echo "Current month representation, having leading zero in 2 digit is: " . date("m"); echo "\n"; echo "The Syntex representation of current month with leading zero is: " . date("M"); echo "\n"; echo "Current month representation,not having leading zero in 2 digit is: " . date("n"); ?>
Current month representation, having leading zero in 2 digits is: 12 The Syntex representation of the current month with leading zero is: Dec Current month representation, not having leading zero in 2 digits is: 12
d – Represents the month’s day. Two numerals with leading zeros are used (01 or 31).
D — In the text, represents the day of the week ( Mon to Sun ).
m — The month is represented by the letter m in numerals with leading zeros (01 or 12).
M — In the text, M stands for month and is shortened ( Jan to Dec ).
y – Denotes a two-digit year (07 or 21).
Y — Year in four numbers is represented by the letter Y.
Using strtotime() and date() Functions to Get the Current Month of a Date in PHP
We’ll go through two steps to get the month from any date using the strtotime() method.
To begin, transform a date to its timestamp equal. Use the date() function with the formatting character to get the month from that timestamp.
php $timestamp = strtotime("5th September 2003"); echo "Current month representation, having leading zero in 2 digits is: " . date("m", $timestamp); echo "\n"; echo "The Syntex representation of current month with leading zero is: " . date("M", $timestamp); echo "\n"; echo "Current month representation,not having leading zero in 2 digits is: " . date("n", $timestamp); ?>
Current month representation, having leading zero in 2 digits is: 09 The Syntex representation of the current month with leading zero is: Sep Current month representation,not having leading zero in 2 digits is: 9
Get the Current Month by Using the DateTime Class in PHP
PHP 5.2 introduces certain pre-built classes to assist developers in solving common problems. DateTime is one of the classes, and it deals with date and time difficulties. Follow these two steps to retrieve the current month using the DateTime class:
First, create a DateTime() class object. The current time is represented when the DateTime() class is used without any parameters.
Then, use the DateTime() class’s format() function to get the year from the newly formed object.
php $now = new DateTime(); echo "Current month representation, having leading zero in 2 digit is: " . $now->format('m'); echo "\n"; echo "The Syntex representation of current month with leading zero is: " . $now->format('M'); echo "\n"; echo "Current month representation,not having leading zero in 2 digit is: " . $now->format('n'); ?>
The 2 digit representation of the current month with leading zero is: 12 The textual representation of the current month with leading zero is: Dec The 2 digit representation of the current month without leading zero is: 12
Related Article — PHP DateTime
How to Get Month From a Date in PHP?
New here? Like SchoolsOfWeb on Facebook to stay up to date with new posts.
Problem:
You have a date which may consist of day, month, and year. Now, You just want to retrieve only the month from that date.
Solution:
There are few different ways to retrieve month part from a date. In the following, you’ll learn methods to retrieve month portion from current date or from any date–
Method 1: Using date() function to retrieve current month
PHP’s date() function can let you know date and time related information based on the formatting characters it takes in its first parameter. The function can take maximum of two parameters. If you use only one parameter, It will return information related to current time.
You can use three different formatting characters in the first parameter of the date() function to get three different forms of a month. These formatting characters are-
- m- To represent a month as a number with leading zero. Ex. 01, 12
- M – To represent a month as short text of three letters. Ex. Jun
- n – To represent a month as a number without leading zero. Ex. 1, 12
See the formatting characters in action in the example below-
"; echo "The textual representation of current month with leading zero is: " . date("M"); echo "
"; echo "The 2 representation digit of current month without leading zero is: " . date("n"); ?>
Output:
The 2 digit representation of current month with leading zero is: 06
The textual representation of current month with leading zero is: Jun
The 2 representation digit of current month without leading zero is: 6
Method 2: Using strtotime() and date() function to retrieve month from any date
Using strtotime() function, to get the month from any date, we’ll follow two steps-
- First convert a date to its equivalent timestamp. A timestamp of a date is the representation of seconds from January 1 1970 00:00:00 UTC to that time, then,
- Use the date() function and the formatting character to retrieve month from that timestamp.
Now, see it in action in the following example-
"; echo "The textual representation of current month with leading zero is: " . date("M", $timestamp); echo "
"; echo "The 2 digit representation of current month without leading zero is: " . date("n", $timestamp); ?>
Output:
The 2 digit representation of current month with leading zero is: 09
The textual representation of current month with leading zero is: Sep
The 2 digit representation of current month without leading zero is: 9
In the above example, I used 9th September 2003 as date format as a sample. You can use any date format you want as long as it follows the supported date and time formats
Method 3: Using DateTime class to get current month
From PHP 5.2, PHP provides some ready-made classes to help developers to solve daily problems they face. One of those classes is DateTime class which solves date time related issues.
To get current month using DateTime class, follow the two steps-
- Create an object of DateTime() class. When you use DateTIme() class without any parameter, it represents the current time.
- Then, use format() method of the DateTime() class to retrieve the year from the newly created object.
See the following example-
format('m'); echo "
"; echo "The textual representation of current month with leading zero is: " . $now->format('M'); echo "
"; echo "The 2 digit representation of current month without leading zero is: " . $now->format('n'); ?>
Output:
The 2 digit representation of current month with leading zero is: 06
The textual representation of current month with leading zero is: Jun
The 2 digit representation of current month without leading zero is: 6
Method 4: Using CreateFromFormat() method to get month from any date
In this method, we’ll retrieve month from any date in the following two steps-
- First, create a DateTime object from the createFromFormat() method of DateTime class using your supplied date, then
- Retrieve month from that object using format() method mentioning formatting parameter “m”, “M”, or “n” in the method’s parameter.
See it in action in the following example –
format('m'); echo "
"; echo "The textual representation of current month with leading zero is: " . $dateObj->format('M'); echo "
"; echo "The 2 digit representation of current month without leading zero is: " . $dateObj->format('n'); ?>
Output:
The 2 digit representation of current month with leading zero is: 02
The textual representation of current month with leading zero is: Feb
The 2 digit representation of current month without leading zero is: 2