50

Possible Duplicate:
PHP: Convert uncommon date format to timestamp in most efficient manner possible?

How to convert this string to datetime format? 06/Oct/2011:19:00:02

I tried this but it doesn't work.

$s = '06/Oct/2011:19:00:02';

$date = strtotime($s);
echo date('d/M/Y:H:i:s', $date);
Community
  • 1
  • 1
emeraldhieu
  • 9,380
  • 19
  • 81
  • 139

2 Answers2

76

Use DateTime::createFromFormat

$date = date_create_from_format('d/m/Y:H:i:s', $s);
$date->getTimestamp();
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
Criss
  • 1,171
  • 9
  • 12
  • 4
    I think you mean `m` not `M`. – Wobbles Aug 11 '16 at 13:21
  • 1
    @Wobbles Nice spotted – Nam G VU Jan 11 '18 at 03:47
  • It should be `M` (or `F`) as stated in the official documentation. @NamGVU please, do not edit greatly accepted answer, if you are not willing to retest the code by yourself. If your MWE yields output other then suggested by official maintainer, please consult it in separate thread or so. – TomiL Feb 22 '21 at 18:31
  • > o not edit greatly accepted answer I don't think so @TomiL - things change thru time – Nam G VU Mar 12 '21 at 03:13
34

The Problem is with your code formatting,

inorder to use strtotime() You should replace '06/Oct/2011:19:00:02' with 06/10/2011 19:00:02 and date('d/M/Y:H:i:s', $date); with date('d/M/Y H:i:s', $date);. Note the spaces in between.

So the final code looks like this

$s = '06/10/2011 19:00:02';
$date = strtotime($s);
echo date('d/M/Y H:i:s', $date);
nbk
  • 1,992
  • 2
  • 19
  • 34