PHP Date and Time
The PHP date()
function serves for formatting a date or time.
The PHP Date() Function
The PHP date()
method converts timestamp to a date and time that is more legible.
Syntax :-
date(format,timestamp)
Parameter | Description |
---|---|
format | Required.The timestamp's format is specified here. |
timestamp | Optional.A timestamp is specified. |
A timestamp is a character sequence that indicates the date and/or time at which a given event took place.
Get a Date
The needed date()
format argument defines how the date is formatted (or time).
The following are some examples of date characters :-
- d - The month's day is represented by this symbol (01 to 31).
- m - A month is represented by this symbol (01 to 12).
- Y - A year is represented by this symbol (in four digits).
- l (lowercase 'L') - The weekday is represented by this symbol.
Additional formatting can be added by inserting characters such as "/", ".", or "-" between the text.
Example :- Today's date is formatted three different ways in the example below :
";
echo "Today is " . date("Y.m.d") . "
";
echo "Today is " . date("Y-m-d") . "
";
echo "Today is " . date("l");
?>
Output :-
Today is 2024.9.7
Today is 2024-9-7
PHP Tip - Automatic Copyright Year
To automatically change the patent year from your website, use the date()
function :
Example :- To keep your website's copyright year current, use the date()
function :
© 2010-
Output :-
Related Links
Get a Time
Here are some examples of characters that are often used to indicate the passage of time :
- H - An hour is formatted in a 24-hour format (00 to 23).
- h - Leading zeros are used in a 12-hour format (01 to 12).
- i - Leading zeros in minutes (00 to 59).
- s - Leading zeros in seconds (00 to 59).
- a - Ante meridiem and Post meridiem are both written in lowercase letters (am or pm).
Example :- The following example displays the current time in the format you specify :-
Output :-
It should be noted that the PHP date()
method returns the server's current date and time!.
Get Your Time Zone
If the time returned by the code is incorrect, it is most likely due to your server being located in another country or being configured for a different timezone.
So you may set the timezone you wish to utilize if you need to be right according to one particular region.
Example :- The example above sets "America/New York" to the time zone, and then the current time is in the format requested :
Related Links
Create a Date With mktime()
The following example changes the timezone to "America/New York" and then prints the current time in the format given.
For a date, the Unix timestamp is returned with the PHP mktime()
feature. The time template for Unix includes the number of seconds (1 January 1970 00:00:00 GMT) between the Unix Epoque and the time.
Syntax :-
mktime(hour, minute, second, month, day, year)
Example :- The following example uses the date()
function to generate a date and time from the mktime()
method's parameters :
Create a Date From a String With strtotime()
PHP strtotime()
is used to convert a date string understandable by humans to a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).
Syntax :-
strtotime(time, now)
Example 1 :- From the strtotime()
function, the following example creates a date and time :
Example 2 :- PHP does a great job of converting a string to a date, allowing you to use a variety of values :
";
$d=strtotime("next Saturday");
echo date("Y-m-d h:i:sa", $d) . "
";
$d=strtotime("+3 Months");
echo date("Y-m-d h:i:sa", $d) . "
";
?>
Strtotime()
isn't ideal though, so remember to verify the strings.
More Date Examples
Example 1 :- The following example shows the dates for the next six Saturdays :
";
$startdate = strtotime("+1 week", $startdate);
}
?>
Example 2 :- The following example calculates the number of days remaining till July 4th :