Jump to content

Condensing Code For Dates


Ninjakreborn

Recommended Posts

<?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.

Link to comment
https://forums.phpfreaks.com/topic/272614-condensing-code-for-dates/
Share on other sites

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'));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.