Вопрос

hello guys i have date like this

25 March 2014 - 16:45

i want to convert this date in to this format how to do that please Help

2014-03-25 16:45:00

i try this $creation = date('Y-m-d H:i:s',strtotime('25 March 2014 - 16:45')); but it's not work

Это было полезно?

Решение 2

Use PHP DateTime

You have "-" in "25 March 2014 - 16:45" in the string so it not be able to read by DateTime

So work around would be

$str = "25 March 2014 -16:45";

$date = new DateTime(str_replace("-","",$str));
echo $date->format('Y-m-d H:i:s');

Другие советы

i try this $creation = date('Y-m-d H:i:s',strtotime('25 March 2014 - 16:45')); but it's not work

The reason your code doesn't work is because '25 March 2014 - 16:45' is not in a format that strtotime() can parse.

strtottime() is good at handling a wide range of formats, but it can't cope with absolutely anything; there's just too much variation possible.

I suggest that you try PHP's DateTime class instead. This class has a method called createFromFormat() which allows you to specify the format of the incoming date string. This makes it easier for it to parse, and allows for formats that might not be recognised otherwise, like yours.

Try this:

$date = DateTime::createFromFormat('j F Y - H:i', '25 March 2014 - 16:45');
echo $date->format('Y-m-d H:i:s');

I've successfully tried the following with your string:

$a = strptime('25 March 2014 - 16:45', '%d %B %Y - %R');
$time = mktime(
    $a["tm_hour"],
    $a["tm_min"],
    $a["tm_sec"],
    $a["tm_mon"]+1,
    $a["tm_mday"],
    $a["tm_year"]+1900
);
$converted = date('Y-m-d H:i:s',$time);

Try this:

$creation = date('Y-m-d H:i:s',strtotime('25 March 2014 16:45'));
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top