note 100925 deleted from function.date-diff by danbrown
| From: | danbrown@php.net | Date: | Mon, 15 Nov 2010 22:38:55 +0000 |
| Subject: | note 100925 deleted from function.date-diff by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174051@lists.php.net to get a copy of this message | ||
Note Submitter: Sergio Abreu
----
<?php
/*
Extra functions for The Sven Obser's workaround on DateInterval for PHP < 5.3
Author: Sergio Abreu
Page: sites.sitesbr.net
Thanks, Sven Obser, for your great workaround for DateInterval !
I'd like to post here some extra functions for extending your code, to give it the static
method DateInterval::createFromFormat($format, $date, $object).
I wrote a function called maskdate with a by reference third parameter ( $format, $date, &$obj).
The $format param can be any combination using (d,j,m,n,y,y,g,h,i,s, spaces,-, / and
':'). The $date param expects any combinations of numbers and date
separators(space,:,/,-). So, $date parameter should not use any textual values as January,etc - only
numbers.
If you use Sven's DateInterval workaround (for servers that run PHP < 5.3) , ADD this
fragment inside the class:
*/
public static function createFromFormat( $format, $date, &$obj){
$new = new DateInterval();
maskdate( $format, $date, $new);
$obj = $new;
}
/*
Here the mask applies correct values to the object:
*/
function maskdate( $format, $date, &$obj){
if( preg_match("/\d/", $format) || preg_match("/[a-z]/i", $date)){
die("maskdate usage: format[[a-z]\-\/\ :] date[\\d+\-\/:]");
}
preg_match_all( "/[a-z]/i", $format, $matches);
$matches = $matches[0];
preg_match_all( "/\d+/", $date, $matches2);
$matches2 = $matches2[0];
foreach( $matches as $k=>$v ){
switch( strtolower( $v)){
case "j" : // same as d
case "d" : $obj->d = intval( $matches2[$k]); break;
case "n" : // same as m
case "m" : $obj->m = intval( $matches2[$k]); break;
case "y" : $obj->y = intval( $matches2[$k]); break;
case "g" : // same as h
case "h" : $obj->h = intval( $matches2[$k]); break;
case "i" : $obj->i = intval( $matches2[$k]); break;
case "s" : $obj->s = intval( $matches2[$k]); break;
}
}
}
?>