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:

10 Comments

  • Claudio |

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

    Abraço

  • energie2b |

    [PHP5.3] function split() deprecated

    Thanks for the class 🙂

    Now, use explode function like this :
    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    /**
    *
    * Class dateOperations
    * Created in July 07, 2009, by Vicente Russo Neto
    *
    * @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) = explode(“-“, $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) = explode(“-“, $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;

    }

    }

    ?>

  • david |

    Parse error: in C:\xampp\htdocs\citisacco\application\libraries\dateOperations.php on line 56 after receiving first error and using explode php 5.3

  • gambar alat bantu untuk wanita |

    Very interesting article, I really like what shering comrades on this page.

So, what do you think ?