note 90423 added to function.date

From: Date: Tue, 21 Apr 2009 20:34:40 +0000
Subject: note 90423 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-153556@lists.php.net to get a copy of this message
Thanks to tcasparr at gmail dot com for the great idea (at least for me) ;) I changed the code a little to replicate the functionality of date_parse_from_format, once I don't have PHP 5.3.0 yet. This might be useful for someone. Hope you don't mind changing your code tcasparr at gmail dot com. <?php /******************************************************* * Simple function to take in a date format and return array of associated * formats for each date element * * @return array * @param string $strFormat * * Example: Y/m/d g:i:s becomes * Array * ( * [year] => Y * [month] => m * [day] => d * [hour] => g * [minute] => i * [second] => s * ) * * This function is needed for PHP < 5.3.0 ********************************************************/ function dateParseFromFormat($stFormat, $stData) { $aDataRet = array(); $aPieces = split('[:/.\ \-]', $stFormat); $aDatePart = split('[:/.\ \-]', $stData); foreach($aPieces as $key=>$chPiece) { switch ($chPiece) { case 'd': case 'j': $aDataRet['day'] = $aDatePart[$key]; break; case 'F': case 'M': case 'm': case 'n': $aDataRet['month'] = $aDatePart[$key]; break; case 'o': case 'Y': case 'y': $aDataRet['year'] = $aDatePart[$key]; break; case 'g': case 'G': case 'h': case 'H': $aDataRet['hour'] = $aDatePart[$key]; break; case 'i': $aDataRet['minute'] = $aDatePart[$key]; break; case 's': $aDataRet['second'] = $aDatePart[$key]; break; } } return $aDataRet; } ?> Also, if you need to change the format of dates: <?php function changeDateFormat($stDate,$stFormatFrom,$stFormatTo) { // When PHP 5.3.0 becomes available to me //$date = date_parse_from_format($stFormatFrom,$stDate); //For now I use the function above $date = dateParseFromFormat($stFormatFrom,$stDate); return date($stFormatTo,mktime($date['hour'], $date['minute'], $date['second'], $date['month'], $date['day'], $date['year'])); } ?> ---- Server IP: 200.185.126.110 Probable Submitter: 187.27.243.75 ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- https://master.php.net/note/edit/90423 Del: integrated -- https://master.php.net/note/delete/90423/integrated Del: useless -- https://master.php.net/note/delete/90423/useless Del: bad code -- https://master.php.net/note/delete/90423/bad+code Del: spam -- https://master.php.net/note/delete/90423/spam Del: non-english -- https://master.php.net/note/delete/90423/non-english Del: in docs -- https://master.php.net/note/delete/90423/in+docs Del: other reasons-- https://master.php.net/note/delete/90423 Reject -- https://master.php.net/note/reject/90423 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#153556) next »