PHP's Date()
function manages date, time, or both. This function modifies the timestamp to a more understandable date and time.
date(format,timestamp)
format
: This parameter defines the structure of the timestamp.
timestamp
: This defines a timestamp. By default, it is the current date and time. This is an optional parameter.
Some commonly used patterns for the format
parameter are the following:
d
: Day (01-31)m
: Month (1-12)y
: Year (4 digits)l
: Day of the week<?phpecho "Day rightnow is " . date("l") ."\n";echo "Today's Date: " . date("Y.m.d") ."\n";echo "Today's Date: " . date("Y/m/d") ."\n";echo "Today's Date: " . date("Y-m-d") ;?>
l
in the date()
function.Year-Month-Day
format with different separators. These separators are .
, /
, and -
.Some commonly used patterns for the format
parameter are the following:
H
for 24-hour time formath
for 12-hour time formati
for minutess
for secondsa
for lowercase ante meridiem and post meridiem (am or pm)
<?phpecho "Current time in 24hr format is " . date("H:i:sa")."\n";echo "Current time in 12hr format is " . date("h:i:sa");?>
date()
function. H
specifies the 24-hour format.date()
function. h
specifies the 12-hour format.<?phpdate_default_timezone_set("America/Chicago");echo "The current time in Chicago is " . date("h:i:sa");?>
date_default_timezone_set()
function sets the time zone according to the country and the state/city defined in the parameters. date()
function prints the current time according to the time zone set above.The mktime()
function returns the Unix timestamp, i.e., the difference in seconds between the time defined and the Unix Epoch (January 1 1970 00:00:00 GMT).
mktime(hour, minute, second, month, day, year)
<?php$new_date=mktime(10, 49, 35, 4, 14, 2022);echo "Created date is " . date("Y/m/d h:i:sa", $new_date);?>
mktime()
function sets the custom time.date()
function and by passing the new_date
object to it.