Skip to content
Advertisement

How can I get today’s timestamp in PHP

I tried

$dtToday = DateTime::createFromFormat('Y-m-d', date('Y-m-d'));

but when I output it

die($dtToday->format('d M Y g:i:s a'));

I still get the time eg “22 Jan 2011 4:53:59 pm”. Why is that?

UPDATE

Ah… many people misunderstood me, my bad, I forgot to point out the main point. I created the date with just the date portion, I don’t want the time. So I’d expect something like

22 Jan 2011 12:00:00 am

Advertisement

Answer

You can call ->setTime(0, 0) to zero out the time portion:

$date = DateTime::createFromFormat('Y-m-d', '2011-01-22')->setTime(0, 0);
echo $date->format('d M Y g:i:s a');
// 22 Jan 2011 12:00:00 am
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement