note 100925 added to function.date-diff

From: Date: Mon, 15 Nov 2010 22:28:07 +0000
Subject: note 100925 added to function.date-diff
Groups: php.notes 
Request: Send a blank email to php-notes+get-174050@lists.php.net to get a copy of this message
<?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; } } } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 189.79.164.34 ---- Manual Page -- http://www.php.net/manual/en/function.date-diff.php Edit -- https://master.php.net/note/edit/100925 Del: integrated -- https://master.php.net/note/delete/100925/integrated Del: useless -- https://master.php.net/note/delete/100925/useless Del: bad code -- https://master.php.net/note/delete/100925/bad+code Del: spam -- https://master.php.net/note/delete/100925/spam Del: non-english -- https://master.php.net/note/delete/100925/non-english Del: in docs -- https://master.php.net/note/delete/100925/in+docs Del: other reasons-- https://master.php.net/note/delete/100925 Reject -- https://master.php.net/note/reject/100925 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#174050) next »