0

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;
);
  • 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 C 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
4

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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