1

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

Dexter
  • 1,804
  • 4
  • 24
  • 53

4 Answers4

4

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');
Spudley
  • 166,037
  • 39
  • 233
  • 307
3

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');
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
2

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);
blue
  • 1,939
  • 1
  • 11
  • 8
  • 1
    Please, note that strptime() is irrelevant to Windows. Anyway, you could use that script on any other OS. – blue Mar 25 '14 at 11:27
0

Try this:

$creation = date('Y-m-d H:i:s',strtotime('25 March 2014 16:45'));
Brian Tyndall
  • 186
  • 3
  • 12
  • but from post value are coming like this 25 March 2014 - 16:45 – Dexter Mar 25 '14 at 11:13
  • The output shows as 2014-03-25 16:45:00, the dash in the strtotime function call was invalidating the date, reverting it to 1969. You can filter the string to remove the dash to make it compliant. To keep it simple look at the str_replace function. – Brian Tyndall Mar 25 '14 at 11:14