CodeIgniter: Date Manipulation Class

I made myself a class make some basic operations, like adding and subtracting days, months or years from a date, and return in mysql-ready format or timestamp. Here`s the code:

[cc lang=”php”]

*
* @param integer $date – the date to be processed
* @param string $what – what piece of date to process, day, month or year
* @param integer $value – how much will be increased or decreased
* @param string $return_format – ‘mysql’ format or ‘timestamp’ format
* @author Vicente Russo Neto
* @return string|boolean
* @version 0.1
*
* Description: This class can add or subtract days, months or years and return the result. Created for
* PHP Framework CodeIgniter (www.codeigniter.com). Tested on 1.7.1.
*
* Usage:
* $this->load->library(‘dateoperations’);
* echo $this->dateoperations->subtract(‘2009-07-01′,’day’,1); // Prints: 2009-06-30
* echo $this->dateoperations->sum(‘2009-07-01′,’year’,1); // Prints: 2010-07-01
* echo $this->dateoperations->subtract(‘2009-07-01′,’day’,15); // Prints: 2009-06-16
*/

class dateOperations {

function sum($date,$what=FALSE,$value,$return_format=’mysql’) {

list($year, $month, $day) = split(“-“, $date);

if ($what!=’day’ && $what!=’month’ && $what!=’year’) return false;

if ($what==’day’) $day = $day + intval($value);
if ($what==’month’) $month = $month + intval($value);
if ($what==’year’) $year = $year + intval($value);

$date_tmp = mktime(0, 0, 0, $month, $day, $year);

if ($return_format==’mysql’) {
$date_tmp = date(‘Y-m-d’, $date_tmp);
} elseif (!$return_format==’timestamp’) {
return false;
}

return $date_tmp;

}

function subtract($date,$what=FALSE,$value,$return_format=’mysql’) {

list($year, $month, $day) = split(“-“, $date);

if ($what!=’day’ && $what!=’month’ && $what!=’year’) return false;

if ($what==’day’) $day = $day – intval($value);
if ($what==’month’) $month = $month – intval($value);
if ($what==’year’) $year = $year – intval($value);

$date_tmp = mktime(0, 0, 0, $month, $day, $year);

if ($return_format==’mysql’) {
$date_tmp = date(‘Y-m-d’, $date_tmp);
} elseif (!$return_format==’timestamp’) {
return false;
}

return $date_tmp;

}

}

?>
[/cc]

Put the dateOperations.php file on application/libraries folder.

Usage examples:

[cc lang=”php”]
load->library(‘dateoperations’);
echo $this->dateoperations->subtract(‘2009-07-01′,’day’,1); // Prints: 2009-06-30
echo $this->dateoperations->sum(‘2009-07-01′,’year’,1); // Prints: 2010-07-01
echo $this->dateoperations->subtract(‘2009-07-01′,’day’,15); // Prints: 2009-06-16

?>
[/cc]

Download the library

Did you like this? Share it:

3,886 Comments

  • phan mem ban hang |

    I’ll immediately clutch your rss as I can not in finding your
    e-mail subscription hyperlink or e-newsletter service.
    Do you’ve any? Please permit me understand so that I may just subscribe.
    Thanks.

  • Josh Taylor Tywardreath |

    Hey this is somewhat of off topic but I was wanting
    to know if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding know-how so I wanted to get advice from someone with experience.
    Any help would be greatly appreciated!

  • pkv game terpercaya |

    Right here is the perfect site for anybody who wants to understand
    this topic. You realize so much its almost hard to argue with
    you (not that I really will need to…HaHa). You definitely put a brand new
    spin on a subject which has been written about for decades.
    Excellent stuff, just excellent!

  • phan mem ban hang |

    Can I simply say what a relief to discover someone who
    truly knows what they’re discussing online. You certainly understand how to bring an issue to light and make it important.
    A lot more people need to check this out and understand this side of the story.
    It’s surprising you aren’t more popular because you definitely possess
    the gift.

  • google maps seo services |

    Wonderful story, reckoned we could combine a couple of unrelated data, nevertheless actually really worth taking a search, whoa did one find out about Mid East has got additional problerms too

  • agence digitale paris |

    we like to honor several other net web sites around the web, even when they arent linked to us, by linking to them. Beneath are some webpages really worth checking out

  • https://bit.ly/38G5OXR |

    Hi, Neat post. There is an issue together with your
    site in web explorer, could check this? IE still is the marketplace leader and a large part of folks will omit
    your fantastic writing due to this problem.

  • https://www.gtainside.com/user/alex41 |

    Just wish to say your article is as amazing.
    The clearness for your publish is simply cool and that i can think you are knowledgeable on this subject.
    Well together with your permission allow me to clutch your feed to keep up to
    date with impending post. Thanks a million and please keep up the enjoyable work.

  • dao tao seo |

    Have you ever considered about adding a little bit more than just your articles?
    I mean, what you say is fundamental and everything.
    However think about if you added some great images or videos to give your posts more, “pop”!
    Your content is excellent but with images and clips, this site could certainly be one of the best in its field.
    Terrific blog!

  • top bach khoa |

    Quality articles or reviews is the crucial to interest the visitors to
    pay a visit the website, that’s what this web site is providing.

  • aurumplanners.com |

    After all, it is not a hardship on visitors to pick up on what you’re taking a look at for the Internet by simply intercepting the signals.
    DS 1 Sheet posters have printing on both top and the back of the paper
    stock, while using printing on the backside finding myself precise
    alignment with this on top as well as a mirror image from the printing on the
    front side. There are corresponding cosplay costumes and also the cosplay accessories for each ones
    with the same time.

  • trunnion ball valve |

    I think that is among the so much significant info for me.

    And i’m satisfied studying your article. However want to statement on few normal issues, The web site style is perfect, the articles is
    really excellent : D. Good task, cheers

  • live chat ban hang |

    Hi, i think that i noticed you visited my weblog thus i got here to return the prefer?.I am attempting to find
    issues to improve my web site!I suppose its ok to use some of
    your ideas!!

  • generic viagra usa |

    truly video viagra for sale the advertising viagra for sale certainly hello viagra
    for sale canadian slightly strain [url=http://viacheapusa.com/#]viagra usa[/url]
    exactly function viagra usa forever remote

  • yeezy boost 500 |

    I am commenting to make you understand what a notable discovery our child developed going through your blog. She realized too many things, including what it’s like to have an ideal helping mindset to get the others without hassle learn about several specialized subject areas. You undoubtedly did more than people’s expectations. Thank you for rendering the practical, safe, educational and in addition easy guidance on the topic to Jane.

  • thiet ke web |

    Just desire to say your article is as surprising.
    The clarity in your post is simply great and i can assume you
    are an expert on this subject. Fine with your
    permission let me to grab your feed to keep updated with forthcoming post.

    Thanks a million and please keep up the rewarding work.

So, what do you think ?