Ninjakreborn Posted January 2, 2013 Share Posted January 2, 2013 <?php $dateTo = $this->input->post('dateFrom'); $dateTo = strtotime($dateTo . "+1 year"); $dateTo = date('m/d/Y', $dateTo); $dateTo = strtotime($dateTo . "-1 day"); $dateTo = date('Y-m-d', $dateTo); ?> Is there a better way to write/condense this and still retail accuracy? This takes a date, adds a year to it, then takes away one day. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 2, 2013 Share Posted January 2, 2013 $dt = '12/25/2012'; $dtObj = new datetime($dt); $dtObj->add(new DateInterval('P1Y')); $dtObj->sub(new DateInterval('P1D')); echo $dtObj->format('m/d/Y'); //--> 12/24/2013 Quote Link to comment Share on other sites More sharing options...
cpd Posted January 2, 2013 Share Posted January 2, 2013 Take note of the american date format. You can chain the add and subject as shown below just be careful it doesn't return false; that said if it does return false in this situation I'd argue its a programming error and you should sort it out so chaining is fine. $dtObj->add(new DateInterval('P1Y'))->sub(new DateInterval('P1D')); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.