我想要更改当前日期,或者想要在当前日期中放置变量,如下所示:
$companyDates = $company_dates['dates']; //this variable come from DataBase
$databaseDate=DateTime::createFromFormat("Y-m-d", $companyDates);
$day=$databaseDate->format('d'); // this is how i take just days from date format(y-m-d)
$dt = new DateTime();
$today = $dt->format('Y-m-d');
$oldDate=$dt->format('Y-m-$day'); // here i want days from database into current day month and year.
然后我想找到这样的另一天:
$date1=date_create($today);
$date2=date_create($oldDate);
$diff=date_diff($date1,$date2);
这可能吗,或者这是我的正确方法吗?
试试这个:使用 setDate
$day=10;
$dt = new DateTime();
$today = $dt->format('Y-m-d');
$out = new DateTime();
$out->setDate($out->format('Y'), $out->format('m'), $day);
$oldDate= $out->format('Y-m-d');
$d1=new DateTime($oldDate);$d2=new DateTime($today);
$difference = $d1->diff($d2);
echo $difference->format('%r%a days');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句