PHP – Date and Time Functions

PHP – Date and Time Functions

PHP date and time functions are in-built functions to print the date and time, It could be current, today, yesterday, tomorrow, even a few years ago, or future date. You can play with date() function and other date and time creation functions and print any date/time dynamically.

The PHP date() function is used to format the date. You can create any date using the date creation function like date_create(), date_add(), date_format(), etc and then make the human-readable date format using the date() function.

For example, we can use this date() function to store the current date in the database while publishing or updating any article. You can also use it to show the countdown for upcoming events or sales or anything which you want to do.

So, in this article, we will learn about the PHP date and time functions and how we can use them to print the date and time.

PHP date() Function

As I said, the PHP date() function is used to print the human-readable format from local date and time. You just have to specify the format as a parameter in this function.

Syntax:

This is the syntax of the date() function and it has two parameters.

  • format: It is a format that we want to print out. Like YYYY-MM-DD, MM-DD-YYY, etc.
  • timestamp: It is optional parameter, default will print the current PHP date time on the server using time() function.

Let’s see a basic example of the date() function.

Output:
2021-11-27

PHP date_create() Function

The date_create() function is used to create a new date. It will return the new DateTime object and we can use this object to create a new date in any format.

Syntax:

It has two parameters and both are optional and the default is the current time and timezone respectively.

Output:
DateTime Object
(
    [date] => 2021-11-27 00:00:00.000000
    [timezone_type] => 3
    [timezone] => UTC
)

Now we use this object to make any format of date and print a new date format using the date_format() function. Let’s see how you can make it.

PHP date_format() Function

As I said, you can use the date_format() function to convert the format of a date. So it will return a new date formatted according to the specified format.

Syntax:

You have to pass the DateTime object and format of the date that you want to print. Let’s see how we can convert the format of the above date_create() function example.

Output:
2021/11/27

PHP date_diff() Function

Let’s suppose you want to see the difference between the two dates. To get the difference between the two dates, we will use the date_diff() function. It will calculate the difference between two DateTime objects and print them.

Syntax:

Pass the two DateTime object as the first two parameters in this function and the third parameter to get only positive intervals of dates, It is optional.

Output:
-2179 days 

PHP time() Function

The PHP time() function is used to print the current timestamp. It will return the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

Syntax:

The output of the time() function depends upon the default time zone. You can check the default time zone in your php.ini file.

So, if you want to change the time zone then you can change it in the php.ini file or you can also set the time zone programmatically using the date_default_timezone_set() function.

See the following example code to print the current timestamp in seconds.

Output:
1509747892

PHP Get and Set Timezone Programmatically

As I said, we can set the timezone programmatically and print a time accordingly. You need to use the date_default_timezone_set() function and pass the timezone as a parameter to set the timezone.

If you know the timezone then you can pass it as a string in the time function else you need to know to timezone.

Get Timezone Programmatically

To get the timezone list programmatically in PHP, you have to use the listIdentifiers() static method of the DateandTime Zone built-in class. Then you can loop through an array and print the timezone list.

See the following example code to print the list of all timezone.

Using the above code, it will print the all timezone list and now you can use them to set the timezone programmatically using PHP code. Let’s see how you can do that.

Set Timezone Programmatically

Now, we will use the date_default_timezone_set() function to set the timezone using the PHP script code. When you are done with setting the timezone then now you can use all date functions in your code script. It will show you time according to the timezone that you set up.

Look at the following example of setting the timezone programmatically in PHP.

Let’s see some most popular time functions and how we can use them to print a time, make a time, convert a time, micro time, etc.

PHP mktime() Function

The mktime() function returns the timestamp in Unix format for a date. It will return false if any error arise.

Syntax:
Parameter Values
  • hour: Optional. the number of hour
  • minute: Optional. the number of minute
  • second: Optional. the number of second
  • month: Optional. the number of month
  • day: Optional. the number of day
  • year: Optional. the number of year

Let’s take an example to see what was the day on October 3, 1975. To see this we will use mktime() function and specify the date and then use the date() function with the “l” parameter.

Output:
Friday

The mktime() function is also useful for doing date arithmetic and validation. It will automatically calculate the correct value for out-of-range input. Let’s have examples.

Output:
Jan-05-2010
Feb-01-2021

PHP strtotime() Function

The strtotime() function uses to parses an English textual DateTime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

So it will convert the string DateTime to a human-readable timestamp.

Syntax:

Some examples of strtotime() function.

Output:
1638094426
1638112426
1638699226
1638983631
1638144000
1637452800
2021-11-27

PHP microtime() Function

The microtime() function is also very useful. It will print the current Unix timestamp in microseconds. I normally use this function to know the execution time of the PHP script.

Syntax:

It has only one parameter, if you set that parameter as true then it will return the timestamp in float number else in string. Default is false.

Output:
 0.25299200 1638095387 

There are many more useful PHP date and time functions that you can use for different purposes. To see all the list PHP date and time functions click here.

PHP Date and Time Function Parameters Values

The following table shows you the common parameters and their values that you can use while working with date-time functions in PHP.

ParameterDescription
Day Parameters
dThe day of the month (from 01 to 31)
DA textual representation of a day (three letters)
jThe day of the month without leading zeros (1 to 31)
lThe day name of week
wA numeric representation of the day (0 for Sunday, 6 for Saturday)
zThe day of the year (from 0 through 365)
Month Parameters
FA full textual representation of a month (January through December)
mA numeric representation of a month (from 01 to 12)
MA short textual representation of a month (three letters)
nA numeric representation of a month, without leading zeros (1 to 12)
tThe number of days in the given month
Year Parameters
yA two digit representation of a year
YA four digit representation of a year
LWhether it’s a leap year (1 if it is a leap year, 0 otherwise)
oThe ISO-8601 year number
Time Parameters
aLowercase am or pm
AUppercase AM or PM
g12-hour format of an hour (1 to 12)
G24-hour format of an hour (0 to 23)
h12-hour format of an hour (01 to 12
H24-hour format of an hour (00 to 23)
iMinutes with leading zeros (00 to 59)
sSeconds, with leading zeros (00 to 59)
uMicroseconds
eThe timezone identifier (Examples: UTC, GMT, Asia/Kolkata)
rReturn formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)

You May Also Like

About the Author: Aman Mehra

Hey! I'm Aman Mehra and I'm a full-stack developer and have 5+ years of experience. I love coding and help to people with this blog.

Leave a Reply

Your email address will not be published.