我有这个PHP脚本,从日期开始获取年龄:
$bday = new DateTime('1987-04-21'); $today = new DateTime(date('Y-m-d')); $diff = $today->diff($bday); printf('%d Years, %d Months', $diff->y, $diff->m);
现在我不想直接printf,我想把结果放在var中以便在别处使用它.怎么样?
只需替换printf
为sprintf
这样
$var = sprintf('%d Years, %d Months', $diff->y, $diff->m);
sprintf的文档