note 90423 deleted from function.date by peehaa

From: Date: Sun, 28 Apr 2019 10:19:32 +0000
Subject: note 90423 deleted from function.date by peehaa
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-210757@lists.php.net to get a copy of this message
Note Submitter: eduardo at digmotor dot com dot br ---- 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'])); } ?>

« previous php.notes (#210757) next »