please click here for more wordpress cource
PHP provides a variety of functions for working with dates and times. Here are some of the most commonly used:
date()
function: This function is used to format a date string based on a given format. It takes two parameters: the format and the timestamp (optional).time()
function: This function returns the current Unix timestamp (number of seconds since January 1, 1970).strtotime()
function: This function parses a string containing a date/time and returns the Unix timestamp.mktime()
function: This function returns the Unix timestamp for a given date and time.strftime()
function: This function is used to format a date and time based on the user’s locale.gmdate()
function: This function is similar to thedate()
function, but it returns the date and time in Greenwich Mean Time (GMT).date_default_timezone_set()
function: This function is used to set the default timezone for date/time functions.
Example usage:
// Set default timezone
date_default_timezone_set('America/New_York');
// Get current timestamp
$timestamp = time();
// Format timestamp
$date = date('F j, Y, g:i a', $timestamp);
// Parse string to timestamp
$timestamp2 = strtotime('next Monday');
// Create timestamp from date and time
$timestamp3 = mktime(12, 0, 0, 3, 15, 2023);
// Format date/time based on locale
$date2 = strftime('%B %d, %Y', $timestamp3);
// Format date/time in GMT
$date3 = gmdate('Y-m-d H:i:s', $timestamp);
echo $date . "\n";
echo $timestamp2 . "\n";
echo $date2 . "\n";
echo $date3 . "\n";