I'm looking for an elegant/efficient way to take out the time portion of a datetime in format 'Ymd H:i:s', and then add 3 days.
Currently the solution is:
date('Y-m-d 00:00:00', strtotime("+3 days", strtotime('2017-01-23 05:32:12')));
where 2017-01-23 05:32:12
is the date, and this correctly outputs 2017-01-26 00:00:00
.
It just feels like there has to be a better way to do this.
Thanks
DateTime() offers several of ways to do this. None of them are any less verbose than your current method:
// Plain old DateTime()
$date = (new DateTime('2017-01-23 05:32:12'))->modify('+3 days')->format('Y-m-d 00:00:00');
// DateTme using DateInterval to add three days
$date = (new DateTime('2017-01-23 05:32:12'))->add(new DateInterval('P3D'))->format('Y-m-d 00:00:00');
// DateTime setting the date to midnight instead of using 00:00:00
$date = (new DateTime('2017-01-23 05:32:12'))->modify('+3 days')->modify('midnight')->format('Y-m-d H:i:s');
If the date is today you can shorten this a bit:
$date = (new DateTime('+3 days'))->format('Y-m-d 00:00:00');
you can use DateTime
class
$date = new DateTime('2017-01-23 05:32:12');
$date->modify('+3 days');
$outputDateString = $date->format('Y-m-d 00:00:00');
one line:
$date = ( new DateTime('2017-01-23 05:32:12'))->modify('+3 days')->format('Y-m-d 00:00:00');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.