note 46947 deleted from function.date by sean

From: Date: Thu, 28 Oct 2004 14:40:23 +0000
Subject: note 46947 deleted from function.date by sean
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79263@lists.php.net to get a copy of this message
Note Submitter: mihai_axe ---- If you have a date in a custom format but previously formated with php date function and you need to convert it another format you could use this function <?php function str2date ($format,$str,$outformat) { for($i=0;$i<strlen($format);$i++) $f_arr[$i] = substr($format,$i,1); for($i=0;$i<strlen($str);$i++) $str_arr[$i] = substr($str,$i,1); while($fch = array_shift($f_arr)) { if($fch != '%') array_shift($str_arr); else { $key = array_shift($f_arr); $stop = current($f_arr); $val = ""; while($stop !== current($str_arr) ) { $val .= array_shift($str_arr); } $d[$key] = $val; } } return date($outformat,mktime(($d['H']?$d['H']:0),($d['i' ]?$d['i']:0),($d['s']?$d['s']:0),($d['m']?$d['m']: 0),($d['d']?$d['d']:0),($d['Y']?$d['Y']:0))); } ?> it needs % before formating characters and only works with m,d,Y,H,i,s and here is some test code: <?php $str1 = "%m/%d/%Y %H:%i:%s"; $str2 = "10/27/2004 14:01:53"; $str3 = "Y-m-d H:i:s"; if(!empty($_REQUEST['format']) && !empty($_REQUEST['str']) && !empty($_REQUEST['outformat'])) echo str2date($_REQUEST['format'],$_REQUEST['str'],$_RE QUEST['outformat']); echo' <form method="POST"> :<input type="text" name="format" size="30" value="'.($_REQUEST['format']?$_REQUEST['format']: $str1).'"> - format<br> :<input type="text" name="str" size="30" value="'.($_REQUEST['str']?$_REQUEST['str']:$str2) .'"> - str<br> :<input type="text" name="outformat" size="30" value="'.($_REQUEST['outformat']?$_REQUEST['outfor mat']:$str3).'"> - outformat<br> <input type="submit" name="submit" value="transform"> </form> '; ?> ---- Reason: useless example

« previous php.notes (#79263) next »