The shortest way to replace a date in the format "yyyy-mm-dd hh: ii: ss" with "yyyy-mm-05 hh: ii: ss"?

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

I need to change the dates so as not to deduct a few days from the date.

Thanks.

+4
source share
4 answers
 $date = '2012-06-08 11:15:00'; echo date('Ym-05 H:i:s', strtotime($date)); // 2012-06-05 23:15:00 
+5
source

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('Ymd H:i:s'); 

This may not be the shortest, but it is a reliable approach.

+4
source

Use regexp , which will be the shortest way

Agree with @DaveRandom. This is the shortest way.

 preg_replace('/-\d{2} /','-05 ',$str) 
+2
source

The most efficient way (not the best) for this is to use the substr_replace () function.

 $input = '2012-06-08 11:15:00'; $output = substr_replace($input, '05', 8, 2); echo($output); 
0
source

All Articles