mysql2date

Convert given date string into a different format.

Description

(string|int|bool) mysql2date( (string) $format, (string) $date, (bool) $translate = true ); 

$format should be either a PHP date format string, e.g. U for a Unix timestamp, or G for a Unix timestamp assuming that $date is GMT.

If $translate is true then the given date and format string will be passed to date_i18n() for translation.

Returns (string|int|bool)

Formatted date string or Unix timestamp. False if $date is empty.

Parameters (3)

0. $format (string)
Format of the date to return.
1. $date (string)
Date string to convert.
2. $translate — Optional. (bool) => true
Whether the return date should be translated. Default true.

Usage

  1. if ( !function_exists( 'mysql2date' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // Format of the date to return. 
  5. $format = ''; 
  6.  
  7. // Date string to convert. 
  8. $date = ''; 
  9.  
  10. // Whether the return date should be translated. Default true. 
  11. $translate = true; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = mysql2date($format, $date, $translate); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function mysql2date( $format, $date, $translate = true ) { 
  2. if ( empty( $date ) ) 
  3. return false; 
  4.  
  5. if ( 'G' == $format ) 
  6. return strtotime( $date . ' +0000' ); 
  7.  
  8. $i = strtotime( $date ); 
  9.  
  10. if ( 'U' == $format ) 
  11. return $i; 
  12.  
  13. if ( $translate ) 
  14. return date_i18n( $format, $i ); 
  15. else 
  16. return date( $format, $i );