Date Functions in PHP

Date Functions in PHP. There are different built-in functions in PHP. PHP date functions perform processing easily with the date and its relevant operations. This date() function can be utilized to obtain and set the date and the time as per our business conditions. PHP time() function provides timestamp as an output. The function date() can be applied to take the date into the people readable format by using the timestamp. Yes, We can use the PHP date() function in different ways. Practically every programming language has built-in date functions to dispense with the date and the time. In the next section, we will see different types of use of this PHP date() function with their examples.

The Use of the Date Functions in PHP

The time we become to any system, date performs a vital role. For any business controls and processes, we may require to maintain track of the date and time. For example: if we are calculating a catalog item to the system, we require to take the date, so that we can make when a catalog item has been calculated. The date() function can be done to hold the date and the time in the database for business-related methods. A date can be formatted in several methods, for this we can pass the specified parameter to take the wanted date format output. We can use the date() function and several other described date functions in PHP applications or the code.

PHP Date Functions and Examples

This date() function always gets a parameter.

<?php
date(parameter1, parameter2);
?>

Where parameter1 is the necessary parameter that indicated the format of the date and parameter2 is the timestamp that is the free one. parameter1 is the sting where the parameter2 is the long integer.

Get the current date

<?php
echo "Today Date is: " . date("d-M-Y"); 
?>

We can understand the above code, the date(“d-M-Y”) will give us a current date in place of Y (display Year), in place of m (Displays Month), in place of d (Displays Day). If we execute the exact same PHP code on the 30th of Dec 2021, we will take the output respectively, which implies it will provide the current date.

Get the current timestamp

We may have the time() function in PHP to get the timestamp. The timestamp is nothing but it’s a numeric value between the time source to the current time.

<?php
echo "Today Current Timestamp is: ".time();
?>

At any position in time, we can take the timestamp to date and the date to timestamp using PHP functions.

Print the year and the month

<?php
$timeStamp = 1638781640;
echo "Month :  " . date("M",$timeStamp); 
echo ", Year: " . date("Y",$timeStamp);  
?>

strtotime() date functions in PHP

We have strtotime() function. It is a function, we can provide a string to take the date as per specifications. For example, we can pass today’s date, tomorrow’s date, yesterday’s date, like that. Then we can use the output of this function to get the date and the time using the date() function. Let’s see the same with a quick example.

<?php
echo "Today date is : ". date("d-M-Y", strtotime("today")) . "<br>";
echo "Tomorrow date is: ".  date("d-M-Y", strtotime("tomorrow")) . "<br>";
echo "Yesterday date was: ".  date("d-M-Y", strtotime("Yesterday")) 
?>

Get the date from the mktime() function

Yes, we have another function in PHP that is mktime() to sort with the date-related varieties of the web. We can use the different numeric parameters with the mktime(). This mktime() over-delivers the output as a timestamp. We can get the date using mktime() function output as a 2nd parameter of the date() function, let’s see the alike with an example.

<?php
$mktime = mktime(10, 30, 59, 30, 12, 2020); 
echo "Year: " . date("d-M-Y, h: i: sa ", $mktime);  
?>

A string of the mktime() parameter values = hours, minutes, seconds, days, months, and years. This order is fixed and predefined, if we will obtain any mistake in passing the parameter then we will have to turn date as an output, so we require to be alert while allotting with this function.

Conclusion

We can clearly say, these date functions in PHP surely perform a vital role in any online application.  It is constantly desirable to keep the timestamp in the database record so that we will have the versatility that we can take the date and time from that as per our requirements.

Scroll to Top