$datenow = new DateTime();
$dn = $datenow -> format("Y-m-d"); //2014-12-02
$yesterday = $datenow -> sub(new DateInterval('P1D')) -> format("Y-m-d"); //2014-12-01
$yestertwo = $datenow -> sub(new DateInterval('P2D')) -> format("Y-m-d"); //2014-11-29
$tomorrow = $datenow -> add(new DateInterval('P1D')) -> format("Y-m-d"); //2014-11-30
$tomotwo = $datenow -> add(new DateInterval('P2D')) -> format("Y-m-d"); //2014-12-02
我必须在这里遗漏一些东西。日期计算似乎已关闭。
更新:
$datenow = new DateTime();
$dn = $datenow -> format("Y-m-d");
$yesterday = $datenow -> sub(new DateInterval('P1D')) -> format("Y-m-d");
$yestertwo = $datenow -> sub(new DateInterval('P1D')) -> format("Y-m-d");
$tomorrow = $datenow -> add(new DateInterval('P3D')) -> format("Y-m-d");
$tomotwo = $datenow -> add(new DateInterval('P1D')) -> format("Y-m-d");
现在输出正确的日期。但是,乍一看,它看起来有点凌乱且难以阅读。有什么解决办法吗?