CodeIgniter: Date Manipulation Class

by Vicente Russo Neto on July 8, 2009

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
*
* Class dateOperations
* Created in July 07, 2009, by Vicente Russo Neto <vicente@vrusso.com.br>
*
* @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 <vicente@vrusso.com.br>
* @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;
       
    }

   
}

?>

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

Usage examples:

1
2
3
4
5
6
7
<?php
$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

?>

Download the library

Did you like this? Share it:

{ 6 comments… read them below or add one }

Claudio October 2, 2009 at 2:26 am

Muito bacana o post, parabéns pelo blog, to acompanhando.

Abraço

Tárcio Zemel October 15, 2009 at 9:44 pm

Excellent class, man! Very good!

Will certainly be useful for me and for others, too!

Thanks for sharing!

retchel February 12, 2010 at 11:41 pm

Thanks it save me! :D Nice job kudos to you!

Vicente Russo Neto February 13, 2010 at 3:58 am

I’m glad it helped, retchel :D

febry January 18, 2011 at 1:56 am

thx bro..

jorge July 16, 2011 at 12:34 am

great work man, thanks for sharing!

Leave a Comment