Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest way to replace date in 'yyyy-mm-dd hh:ii:ss' format with 'yyyy-mm-05 hh:ii:ss'?

Tags:

date

php

time

What is the shortest way to replace date in 'yyyy-mm-dd hh:ii:ss' format with 'yyyy-mm-05 hh:ii:ss'?

I need to change the date days, not to subtract several days from the date.

Thank you.

like image 628
Haradzieniec Avatar asked Dec 20 '22 20:12

Haradzieniec


2 Answers

$date = '2012-06-08 11:15:00';
echo date('Y-m-05 H:i:s', strtotime($date)); // 2012-06-05 23:15:00
like image 113
flowfree Avatar answered May 04 '23 09:05

flowfree


You can go with:

$dt = new DateTime('2012-06-08 12:00:00');
$dt->setDate($dt->format('Y'), $dt->format('m'), 5);
echo $dt->format('Y-m-d H:i:s');

Maybe not the shortest, but it's a solid approach

like image 21
dan-lee Avatar answered May 04 '23 09:05

dan-lee