PHP Date & Time

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:

  1. 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).
  2. time() function: This function returns the current Unix timestamp (number of seconds since January 1, 1970).
  3. strtotime() function: This function parses a string containing a date/time and returns the Unix timestamp.
  4. mktime() function: This function returns the Unix timestamp for a given date and time.
  5. strftime() function: This function is used to format a date and time based on the user’s locale.
  6. gmdate() function: This function is similar to the date() function, but it returns the date and time in Greenwich Mean Time (GMT).
  7. 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";

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *