Convert date to given format from input

I need to convert a given date already stored in database to a certain format. I have been trying to google it but can’t get it working.

The Format as Input: d-j-Y ( 06-16-2015 )

Read More

The Format to be converted in: F j, Y (June 16, 2015)

The code I have been trying to convert,

$date               = get_post_meta( $post_id, 'timeline_event_date', true );
$old_date           = date('d-j-Y', $date);
$old_date_timestamp = strtotime($old_date);
$new_date           = date('F j, Y', $old_date_timestamp);

var_dump(s):

  • var_dump for $date: string(10) "06-16-2015"
  • var_dump for $old_date: string(9) "01-1-1970"
  • var_dump for $new_date: string(15) "January 1, 1970"

EDIT For The Duplicate Question

The duplicate question you marked has the solution to fix this, but I feel this is a good test case for all of WordPress users who are having the same issue ..

Related posts

1 comment

  1. Found the solution, in case someone needs it:

    $date   = get_post_meta( $post_id, 'timeline_event_date', true );
    
    $parsed = date_parse_from_format('n-d-Y', $date);
    
    $old_date_timestamp = mktime(
            $parsed['hour'], 
            $parsed['minute'], 
            $parsed['second'], 
            $parsed['month'], 
            $parsed['day'], 
            $parsed['year']
    );
    
    $new_date = date('F j, Y', $old_date_timestamp);
    

    Basically, this code converts string to proper date format.

Comments are closed.