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:

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”]
< ?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) = 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”]
< ?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

?>
[/cc]

Download the library

Continue Reading

Did you like this? Share it:

Working with large XML files in PHP

There are many ways to manage big XML files in php. When I say large, is really large, 10, 20, 30 mebabytes. If we were open a file like this on a common server, we certainly will have a time-out error. This is because we usually use SimpleXML functions. This extensions is, in fact, a “Tree-based parser”, just like “DOM Parser”. They work great in small-medium XML files (~1MB). They put the whole content on memory, and then, parse it. But when we face a big content, the only option is the “Stream-based parsers”. These are more efficiently and faster, because it read the file on demand, and dont crush your servers memory.

Amongst the stream-based parsers, we have SAX and XMLReader. Ill show you how to read a big XML with XMLReader, because its easier and faster comparing to SAX, as you can se here.

The XMLReader is an extension enabled by default on PHP 5.1 and earlier. Born from XmlTextReader API (C#) and its based om libxml2 library. Before that, the XMLReader extension was only available on PECL. XMLReader supports namespaces and validations, including DTD and Relax NG (REgular LAnguage for XML Next Generation).

Well, lets to the code. Heres me XML example:

PHP code:

See, in determinate point I transform the actual block of data (the users tag) into a SimpleXML object, making the manipulation extremely easy. This way, you can easily work with big XML with no harm to the servers memory and no decrease on speed. My next move: try to optimize this script and maybe create a CodeIgniters library. That certainly will be a nice addon to developers, I think. Of course any help will be very welcome!

Did you like this? Share it: