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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.