Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following $mydate = "26 January, 2012, 5:05 AM";

I want to format this date properly so I can insert it into a post_date. Is there a simple way to do it?

$post_ information = array (
    //other data is also inserted
    'post_date' => $mydate;
);
share|improve this question
    
strtotime would be your best bet but it doesn't work. Where did you get that date string? –  s_ha_dum Apr 18 '13 at 2:55
    
If you can back up a step or two it might be easier. If not, you probably have to parse that string with regex. –  s_ha_dum Apr 18 '13 at 3:00
    
@s_ha_dum I got this from an RSS feed that I am reading. I am basically trying to import the data into a post. Everything works fine except that I can't get the date to format properly. –  John Apr 18 '13 at 3:09
    
In mysql, the accepted date format is Y-m-d H:i:s, u can customized the display of post date in post loop –  Tamil Selvan Apr 18 '13 at 3:10
    
@TamilSelvan I understand about the formatting being Y-m-d H:i:s but... the issue is how do we get "26 January, 2012, 5:05 AM"; to look like Y-m-d H:i:s –  John Apr 18 '13 at 3:37
add comment

1 Answer

up vote 4 down vote accepted

That format is almost readable by strtotime. Remove the commas and it will convert.

$t = '27 January, 2012, 5:05 AM';
$t = str_replace(',','',$t);
$t = strtotime($t);
echo date('Y-m-d H:i:s',$t);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.