PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
In PHP, you can get the current date and time using the date
function, which formats a local date and time and returns it as a string.
Here's a simple example:
echo date('Y-m-d H:i:s'); // 2023-05-15 12:45:30 (for example)
In this example, 'Y' represents a four-digit year, 'm' represents a two-digit month, 'd' represents a two-digit day, 'H' represents hours in 24-hour format, 'i' represents minutes, and 's' represents seconds.
Note that the date
function returns the current date and time according to the default timezone set in the PHP configuration file (php.ini). If you want to get the current date and time for a different timezone, you can use the date_default_timezone_set
function to set the timezone.
Here's an example:
date_default_timezone_set('America/New_York'); echo date('Y-m-d H:i:s'); // Outputs the current date and time in New York
In this example, the date_default_timezone_set
function sets the default timezone to 'America/New_York'. The date
function then returns the current date and time in New York.
Using the DateTime class
Alternatively, you can use the DateTime class to get the current date and time:
$datetime = new DateTime(); echo $datetime->format('Y-m-d H:i:s');
The DateTime class provides more flexibility and additional methods for manipulating dates and times compared to the date
function. However, for simply getting the current date and time, either approach works well.
PHP get current time function:
date()
function to retrieve the current date and time.<?php $currentTime = date('H:i:s'); // Format: 24-hour:minute:second echo "Current Time: $currentTime";
Retrieving current date and time in PHP:
date()
to get the current date and time.<?php $currentDateTime = date('Y-m-d H:i:s'); // Format: Year-month-day 24-hour:minute:second echo "Current Date and Time: $currentDateTime";
Using date() function in PHP for current time:
date()
function formats the current time according to the specified format.<?php $formattedTime = date('h:i A'); // Format: 12-hour:minute AM/PM echo "Formatted Current Time: $formattedTime";
Timezone settings in PHP for current time:
date_default_timezone_set()
.<?php date_default_timezone_set('America/New_York'); $currentDateTimeNY = date('Y-m-d H:i:s'); echo "Current Date and Time in New York: $currentDateTimeNY";
Displaying formatted current time in PHP:
date()
function.<?php $customFormatTime = date('D, d M Y H:i:s'); // Custom format echo "Custom Formatted Time: $customFormatTime";
Getting current timestamp in PHP:
time()
.<?php $timestamp = time(); echo "Current Timestamp: $timestamp";
Converting timestamp to readable time in PHP:
<?php $timestamp = 1644144000; // Example timestamp $convertedTime = date('Y-m-d H:i:s', $timestamp); echo "Converted Time: $convertedTime";
24-hour vs. 12-hour format in PHP time display:
date()
function with different formats for 24-hour and 12-hour time.<?php $time24Hour = date('H:i:s'); // Format: 24-hour:minute:second $time12Hour = date('h:i:s A'); // Format: 12-hour:minute:second AM/PM echo "24-Hour Format: $time24Hour, 12-Hour Format: $time12Hour";
Adding/subtracting time in PHP:
strtotime()
for adding or subtracting time.<?php $currentDate = date('Y-m-d'); $nextWeek = date('Y-m-d', strtotime('+1 week')); echo "Current Date: $currentDate, Next Week: $nextWeek";