CodeIgniter: Date And Time Manipulation Class v0.2

Just made a new version of this class to fit a new project I’m doing. Dropped deprecated functions, added time manipulation and few code redesign.

[cc lang=”php”]

* Modified on November 04, 2010, by Vicente Russo Neto
*
* @param integer $date – the date to be processed
* @param string $what – what piece of date to process. Values: day|month|year|hour|minute|second
* @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.2
*
* Description: This class can add or subtract days, months, years, hours, minutes, seconds and return the result. Created for
* PHP Framework CodeIgniter (www.codeigniter.com). Tested on 1.7.2.
*
* Usage:
* echo ‘
‘.$this->dateoperations->sum(‘2010-11-04′,’day’,1,FALSE); // Prints: 2010-11-05
* echo ‘
‘.$this->dateoperations->sum(‘2010-11-04 00:00:00′,’minute’,15); // Prints: 2010-11-04 00:15:00
* echo ‘
‘.$this->dateoperations->subtract(‘2010-11-04 00:00:00′,’second’,35); // Prints: 2010-11-03 23:59:25
* echo ‘
‘.$this->dateoperations->subtract(‘2010-11-04 00:00:00′,’year’,1); // Prints: 2009-11-04 00:00:00
* echo ‘
‘.$this->dateoperations->subtract(‘2010-11-04 00:00:00′,’day’,15, FALSE); // Prints: 2010-10-20
*/

class dateOperations {

public function sum ($date,$what,$value,$full=TRUE,$return_format=’mysql’) {

$return = $this->calculate($date,$what,$value,’sum’,$full,$return_format);
return $return;

}

public function subtract ($date,$what,$value,$full=TRUE,$return_format=’mysql’) {

$return = $this->calculate($date,$what,$value,’subtract’,$full,$return_format);
return $return;

}

private function calculate($date,$what,$value,$operation,$full,$return_format) {

// checking args
if($operation!=’sum’ && $operation!=’subtract’) return FALSE;
if ($what!=’day’ && $what!=’month’ && $what!=’year’ && $what!=’hour’ && $what!=’minute’ && $what!=’second’) return FALSE;

// validating date or datetime
if (!preg_match(‘/\\A(?:^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|(1[0-9])|(2[0-3]))\\:([0-5][0-9])((\\s)|(\\:([0-5][0-9])))?))?$)\\z/’, $date)) {
return FALSE;
}

/* From this point I’m sure its a valid date or datetime no matter what */

$only_date = substr($date, 0, 10);
$only_time = substr($date, 11, 8);

list($year, $month, $day) = explode(“-“, $only_date);

if ($what==’day’) $day = $operation==’sum’ ? intval($day) + intval($value) : intval($day) – intval($value) ;
if ($what==’month’) $month = $operation==’sum’ ? intval($month) + intval($value) : intval($month) – intval($value);
if ($what==’year’) $year = $operation==’sum’ ? intval($year) + intval($value) : intval($year) – intval($value);

$hour = $minute = $second = 0;

if($only_time!=”) { // we have time too!

list($hour, $minute, $second) = explode(“:”, $only_time);

if ($what==’hour’) $hour = $operation==’sum’ ? intval($hour) + intval($value) : intval($hour) – intval($value);
if ($what==’minute’) $minute = $operation==’sum’ ? intval($minute) + intval($value) : intval($minute) – intval($value);
if ($what==’second’) $second = $operation==’sum’ ? intval($second) + intval($value) : intval($second) – intval($value);

}

$date_tmp = mktime($hour, $minute, $second, $month, $day, $year);

if ($return_format==’mysql’) {
if($full) $date_tmp = date(‘Y-m-d H:i:s’, $date_tmp); // return date and time
else $date_tmp = date(‘Y-m-d’, $date_tmp); // return only date
} elseif (!$return_format==’timestamp’) {
return FALSE;
}

return $date_tmp;

}

}

/* End of file dateOperations.php */
/* Location: ./application/libraries/dateOperations.php */
[/cc]

Put the dateOperations.php file on application/libraries folder.
Usage examples:

[cc lang=”php”]
$this->load->library(‘dateoperations’);

echo ‘
‘.$this->dateoperations->sum(‘2010-11-04′,’day’,1,FALSE); // Prints: 2010-11-05
echo ‘
‘.$this->dateoperations->sum(‘2010-11-04 00:00:00′,’minute’,15); // Prints: 2010-11-04 00:15:00
echo ‘
‘.$this->dateoperations->subtract(‘2010-11-04 00:00:00′,’second’,35); // Prints: 2010-11-03 23:59:25
echo ‘
‘.$this->dateoperations->subtract(‘2010-11-04 00:00:00′,’year’,1); // Prints: 2009-11-04 00:00:00
echo ‘
‘.$this->dateoperations->subtract(‘2010-11-04 00:00:00′,’day’,15, FALSE); // Prints: 2010-10-20
[/cc]

Download dateOperations v0.2

Did you like this? Share it:

94 Comments

  • ps4 games |

    My programmer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the expenses.
    But he’s tryiong none the less. I’ve been using Movable-type
    on numerous websites for about a year and am anxious about switching to
    another platform. I have heard very good things about blogengine.net.
    Is there a way I can import all my wordpress posts into it?

    Any kind of help would be greatly appreciated!

  • air jordan |

    I抦 impressed, I have to say. Really hardly ever do I encounter a weblog that抯 both educative and entertaining, and let me inform you, you may have hit the nail on the head. Your idea is outstanding; the problem is something that not enough persons are talking intelligently about. I’m very pleased that I stumbled throughout this in my seek for one thing referring to this.

  • quest bars cheap |

    Hi there, There’s no doubt that your web site might be having web
    browser compatibility issues. When I look at your blog in Safari, it looks fine however, when opening in IE, it has some overlapping issues.
    I just wanted to provide you with a quick heads up! Besides that,
    fantastic blog!

  • quest bars cheap |

    Hi there would you mind letting me know which webhost you’re utilizing?
    I’ve loaded your blog in 3 completely different internet browsers and I must say this blog loads
    a lot faster then most. Can you recommend a good internet hosting
    provider at a reasonable price? Many thanks, I
    appreciate it!

  • golden goose sneakers |

    I found your blog website on google and examine a few of your early posts. Proceed to keep up the excellent operate. I simply extra up your RSS feed to my MSN Information Reader. In search of ahead to studying more from you afterward!?

  • Google |

    That may be the finish of this write-up. Right here youll uncover some websites that we feel youll appreciate, just click the hyperlinks.

  • Apostle Joshua Selman |

    Wonderful story, reckoned we could combine a couple of unrelated information, nonetheless definitely really worth taking a appear, whoa did one particular find out about Mid East has got additional problerms at the same time

  • ps4 games |

    Does your website have a contact page? I’m having problems locating it but, I’d like to shoot you
    an e-mail. I’ve got some suggestions for your blog you might be interested in hearing.

    Either way, great site and I look forward to seeing it expand over time.

  • Clifton |

    Hello are using WordPress for your blog platform?
    I’m new to the blog world but I’m trying to get started and set up my own. Do you require any html coding knowledge
    to make your own blog? Any help would be greatly appreciated!

  • ps4 games |

    I’m amazed, I must say. Seldom do I encounter a blog that’s both
    equally educative and interesting, and without a doubt, you have
    hit the nail on the head. The problem is something which not enough folks are speaking
    intelligently about. I am very happy I came across this in my search for something relating to this.

  • chrome hearts online store |

    The subsequent time I learn a blog, I hope that it doesnt disappoint me as a lot as this one. I mean, I do know it was my choice to learn, however I truly thought youd have one thing attention-grabbing to say. All I hear is a bunch of whining about one thing that you may repair should you werent too busy in search of attention.

  • Zenaida |

    Excellent website. A lot of helpful info here. I am sending it to a few pals ans also sharing in delicious.
    And of course, thanks on your effort!

  • kratom capsules euphoria |

    Hello! Quick question that’s entirely off topic. Do you know how to make your site mobile friendly? My weblog looks weird when browsing from my iphone4. I’m trying to find a theme or plugin that might be able to correct this problem. If you have any recommendations, please share. Appreciate it! kratom

  • ps4 games |

    It’s hard to find knowledgeable people on this subject, but you sound like you know what
    you’re talking about! Thanks

  • quest bars cheap |

    you’re in point of fact a excellent webmaster. The
    site loading pace is incredible. It sort of feels that you’re doing any distinctive trick.

    Furthermore, The contents are masterpiece. you’ve done a wonderful task in this subject!

  • double dildo |

    Every once inside a though we pick blogs that we study. Listed below would be the most up-to-date internet sites that we decide on

  • quest bars cheap |

    Very nice post. I just stumbled upon your weblog and wanted to say that I have
    truly enjoyed surfing around your blog posts.
    In any case I’ll be subscribing to your feed and I hope you write again soon!

So, what do you think ?