dongle7637 2015-06-14 23:49
浏览 7

从输入[重复]将日期转换为给定格式

This question already has an answer here:

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 )

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 ..

</div>
  • 写回答

1条回答 默认 最新

  • dongzanghua8422 2015-06-15 00:24
    关注

    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.

    评论

报告相同问题?