date_create_from_format

Reimplementation of DateTime::createFromFormat for PHP < 5.3.

Description

date_create_from_format( (string) $date_format, $date_value ); 

:( Borrowed from http://stackoverflow.com/questions/5399075/php-datetimecreatefromformat-in-5-2

Parameters (2)

0. $date_format (string)
The date format.
1. $date_value
The date value.

Usage

  1. if ( !function_exists( 'date_create_from_format' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'maps-builder/vendor/wordimpress/maps-builder-core/includes/libraries/metabox/includes/helper-functions.php'; 
  3.  
  4. // The date format. 
  5. $date_format = ''; 
  6.  
  7. // The date value. 
  8. $date_value = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = date_create_from_format($date_format, $date_value); 
  12.  

Defined (1)

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

/vendor/wordimpress/maps-builder-core/includes/libraries/metabox/includes/helper-functions.php  
  1. function date_create_from_format( $date_format, $date_value ) { 
  2.  
  3. $schedule_format = str_replace( 
  4. array( 'M', 'Y', 'm', 'd', 'H', 'i', 'a' ),  
  5. array('%b', '%Y', '%m', '%d', '%H', '%M', '%p' ),  
  6. $date_format 
  7. ); 
  8.  
  9. /** 
  10. * %Y, %m and %d correspond to date()'s Y m and d. 
  11. * %I corresponds to H, %M to i and %p to a 
  12. */ 
  13. $parsed_time = strptime( $date_value, $schedule_format ); 
  14.  
  15. $ymd = sprintf( 
  16. /** 
  17. * This is a format string that takes six total decimal 
  18. * arguments, then left-pads them with zeros to either 
  19. * 4 or 2 characters, as needed 
  20. */ 
  21. '%04d-%02d-%02d %02d:%02d:%02d',  
  22. $parsed_time['tm_year'] + 1900, // This will be "111", so we need to add 1900. 
  23. $parsed_time['tm_mon'] + 1, // This will be the month minus one, so we add one. 
  24. $parsed_time['tm_mday'],  
  25. $parsed_time['tm_hour'],  
  26. $parsed_time['tm_min'],  
  27. $parsed_time['tm_sec'] 
  28. ); 
  29.  
  30. return new DateTime($ymd);