Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a date in a given format

Tags:

php

datetime

I have a date in this format:

20110122000000.000

how can I change that so it's

12.01.2011

(12 January 2011)

Then I use this in php:

$origDate = "20110112000000.000";
$newDate = date("d.m.Y", strtotime($origDate));

it can output something like:

01.12.2011

Thanks!

like image 371
gorsa Avatar asked Oct 25 '22 21:10

gorsa


1 Answers

If you are using >= PHP 5.3

$date = DateTime::createFromFormat('Ymd', '20110112000000.000');
echo $date->format('d.m.Y');

I can't test PHP 5.3 here, so if that extraneous stuff is playing up createFromFormat(), just remove it first.

I don't know about all your formats, but this will remove all 0 and . at the end of your string.

$date = rtrim($date, '.0')

And because the most significant number is always on the right, (2 in this example), it shouldn't be a problem.

like image 54
alex Avatar answered Oct 31 '22 10:10

alex