How can force change the day in datetime in php
$in = date_create('2013-10-27');
// example 1
$out = date_create($in->format('Y-m-10'));
echo $out->format('Y-m-d') . "\n";
// example 2
$out = clone $in;
$out->setDate($out->format('Y'), $out->format('m'), 10);
echo $out->format('Y-m-d') . "\n";
// example 3
$out = clone $in;
$out->modify((10 - $out->format('d')) . ' day');
echo $out->format('Y-m-d') . "\n";
Demo.
You can use the native PHP "date_date_set" function to make this change.
$date = date_create('2013-10-27');
echo $date->format('Y-m-d');
2013-10-27
date_date_set($date,
date_format($date, 'Y'),
date_format($date, 'm'),
10);
echo $date->format('Y-m-d');
2013-10-10
Or using the Object-Oriented style:
$date = new DateTime('2013-10-27');
echo $date->format('Y-m-d');
2013-10-27
$date->setDate($date->format('Y'), $date->format('m'), 10);
echo $date->format('Y-m-d');
2013-10-10