Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format date in PHP from a string of concatenated numbers?

Tags:

string

date

php

I am new to PHP and I am trying to learn more of php date and time but I seem to get stuck with this.

I have this date format:

 ddMMyyHHmmss

And an example is 120813125055 but I am trying to manipulate the string such that it will give me the format of:

 yyyy-MM-dd HH:mm:ss (in the example above, 2013-08-12 12:50:55)

I tried to do something like:

 date('Y-m-d H:i:s', strtotime('120813125055'));

But it always gives me a result of 1969-12-31 18:00:00.

I assume that I need to do some string manipulation in PHP for this but I was wondering if there is an easier and more efficient way to do it?

like image 802
Atasha Avatar asked Feb 16 '23 14:02

Atasha


1 Answers

I think what you're looking for is in the second response answered here: how to re-format datetime string in php?

To summarize (and apply to your example), you could modify the code like this.

$datetime = "120813125055"; 
$d = DateTime::createFromFormat("dmyHis", $datetime);
echo $d->format("Y-m-d H:i:s");
like image 50
Michael Oliver Avatar answered Feb 18 '23 12:02

Michael Oliver