Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert this string to datetime [duplicate]

Tags:

php

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);
like image 326
emeraldhieu Avatar asked Nov 09 '11 09:11

emeraldhieu


2 Answers

Use DateTime::createFromFormat

$date = date_create_from_format('d/m/Y:H:i:s', $s);
$date->getTimestamp();
like image 158
Criss Avatar answered Oct 16 '22 06:10

Criss


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);
like image 33
nbk Avatar answered Oct 16 '22 07:10

nbk