Get the current date and time in every possible format in PHP
Greeting programmers, in this tutorial we will see how to get the current date and time in PHP.
Often developers need the date and time for various different purposes. For example, we need the date and time when the user clicked to enroll for the program which was opened for a limited time. We can also use it as a countdown of days on the website for displaying the end date and time for the event.
PHP has a variety of built-in methods for getting the current date and time which makes the task simple for the developer.
The most used function to get the current date and time in PHP is the ‘date()’ method in PHP. It prints out the desired date and time as per the input given by the user. It takes another optional parameter which when not given, outputs the date and time in string format. We can change the format using the second parameter.
Getting the date and time in dd/mm/yyyy, hrs:min:sec format
Below is the illustration for getting the current date and time in the default format.
<?php #get current date and time function function getCurrDateTime() { $currDateTime = date("d/m/Y, H:i:s"); return $currDateTime; } #driver echo "The current date and time is: ".getCurrDateTime()."."; ?>
Output
The current date and time is: 17/09/2022, 12:27:02.
Getting the time zone from the system and changing it
We can also get the present time zone from the system using the ‘date_default_timezone_get()’ method. Given below is the illustration for the following.
<?php #get the timezone function getTimezone() { $timezone = date_default_timezone_get(); return $timezone; } #driver echo "The timezone is: ".getTimezone()."."; ?>
Output
The timezone is: Europe/Berlin.
Changing the present time zone to a different time zone
PHP offers another method ‘date_default_timezone_set()’ through which we can change the present time zone to a different place time zone. Given below is the illustration for the following.
<?php #get the timezone function getTimezone() { $timezone = date_default_timezone_get(); return $timezone; } #change the timezone function changeTimezone() { date_default_timezone_set("Asia/Kolkata"); } #driver echo "The timezone is: ".getTimezone().".<br>"; changeTimezone(); echo "The timezone now after changing it to different place time zone is: ".getTimezone()."."; ?>
Output
The timezone is: Europe/Berlin. The timezone now after changing it to different place time zone is: Asia/Kolkata.
Get the date from the date() method
We can acquire very specific values from the date() method in PHP. For example, if the developer wants only the date and not the time, he/she can specify the user preference and the script will print accordingly. Given below is the illustration for the following.
<?php #get current date function getCurrDate() { $currDate = date("d/m/Y"); return $currDate; } #driver echo "The current date is: ".getCurrDate()."."; ?>
Output
The current date is: 17/09/2022.
There are some preformatting data characters that the user can use accordingly to change the format of the output.
‘D’ -- gets the week day with three letter abbreviation ‘l’-- get the full week day name ‘M’ -- gets the month with three letter abbreviation ‘F’ -- gets the full month name ‘y’ -- gets only the two-digit year ‘Y’ -- gets the full year
Given below is the illustration of the above following formats.
<?php #driver echo "The current date is: ".date("d/m/Y").".<br><br>"; echo "The week day with three letter abbreviation is: ".date('D').".<br>"; echo "The week day without abbreviation is: ".date('l').".<br>"; echo "The month with three letter abbreviation is: ".date('M').".<br>"; echo "The month without abbreviation is: ".date('F').".<br>"; echo "The year with two digits is: ".date('y').".<br>"; echo "The full year is: ".date('Y')."."; ?>
Output
The current date is: 17/09/2022. The week day with three letter abbreviation is: Sat. The week day without abbreviation is: Saturday. The month with three letter abbreviation is: Sep. The month without abbreviation is: September. The year with two digits is: 22. The full year is: 2022.
Get the time from the date() method
We can also get only the time from the date() method in PHP. Given below is the default time formatting which we use and get it from the ‘date()’ method.
<?php date_default_timezone_set("Asia/Kolkata"); #get current time function getCurrTime() { $currTime = date("H:i:s"); return $currTime; } #driver echo "The current time is: ".getCurrTime()."."; ?>
Output
The current time is: 16:37:39.
We also have preformatting characters for time which we can use accordingly to change the output format.
‘h’ -- gets hours 12-hr format ‘H’ -- gets hours in 24-hr format ‘a’ -- gets am/pm ‘i’ -- gets minutes ‘s’ -- gets seconds
Given below is the illustration of the above-discussed formatting techniques of the time.
<?php #driver echo "The current time is: ".date("H:i:s").".<br><br>"; echo "The hour in 12-hr format is: ".date("h").".<br>"; echo "The hour in 24-hr format is: ".date("H").".<br>"; echo "The meridian of the time is: ".date("a").".<br>"; echo "The minute is: ".date("i").".<br>"; echo "The second is: ".date("s").".<br>"; ?>
Output
The current date is: 17/09/2022. The week day with three letter abbreviation is: Sat. The week day without abbreviation is: Saturday. The month with three letter abbreviation is: Sep. The month without abbreviation is: September. The year with two digits is: 22. The full year is: 2022.
All the above-discussed date-time formatting techniques in PHP can help the user in getting the required output for manipulations.
Leave a Reply