Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format a date - without time?

Tags:

php

I have a date:

30/12/2010

I use:

DateTime::createFromFormat('d/m/Y', $input['date']);

But this outputs:

2010-12-30 21:15:37

How can I output just the date and not the time?

like image 540
panthro Avatar asked Feb 13 '26 19:02

panthro


2 Answers

You can write a "!" before the format string.

DateTime::createFromFormat('!d/m/Y', $input['date']);

This will set the time to midnight.

like image 64
Oliver Tischlinger Avatar answered Feb 15 '26 08:02

Oliver Tischlinger


In fact, your solution is in your question. createFromFormat method return to us a DateTime object.You can use it to print formatted date time.

$date = DateTime::createFromFormat('d/m/Y', $input['date']);
echo $date->format("Y-m-d");
like image 24
hkulekci Avatar answered Feb 15 '26 07:02

hkulekci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!