note 96424 modified in function.strftime by danbrown

From: Date: Fri, 26 Feb 2010 19:16:48 +0000
Subject: note 96424 modified in function.strftime by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166406@lists.php.net to get a copy of this message
Created this small method to convert a date format to a strftime format. One format is enough to learn :) <?php /** * Convert a date format to a strftime format * * Timezone conversion is done for unix. Windows users must exchange %z and %Z. * * Unsupported date formats : S, n, t, L, B, G, u, e, I, P, Z, c, r * Unsupported strftime formats : %U, %W, %C, %g, %r, %R, %T, %X, %c, %D, %F, %x * * @param string $dateFormat a date format * @return string */ public static function dateFormatToStrftime($dateFormat) { $caracs = array( // Day - no strf eq : S 'd' => '%d', 'D' => '%a', 'j' => '%e', 'l' => '%A', 'N' => '%u', 'w' => '%w', 'z' => '%j', // Week - no date eq : %U, %W 'W' => '%V', // Month - no strf eq : n, t 'F' => '%B', 'm' => '%m', 'M' => '%b', // Year - no strf eq : L; no date eq : %C, %g 'o' => '%G', 'Y' => '%Y', 'y' => '%y', // Time - no strf eq : B, G, u; no date eq : %r, %R, %T, %X 'a' => '%P', 'A' => '%p', 'g' => '%l', 'h' => '%I', 'H' => '%H', 'i' => '%M', 's' => '%S', // Timezone - no strf eq : e, I, P, Z 'O' => '%z', 'T' => '%Z', // Full Date / Time - no strf eq : c, r; no date eq : %c, %D, %F, %x 'U' => '%s' ); return strtr((string)$dateFormat, $caracs); } ?> I use it with this condition on a date format : if(preg_match("/(D|l|F|M)/", $format)) --was-- Created this small method to convert a date format to a strftime format. One format is enough to learn :) /** * Convert a date format to a strftime format * * Timezone conversion is done for unix. Windows users must exchange %z and %Z. * * Unsupported date formats : S, n, t, L, B, G, u, e, I, P, Z, c, r * Unsupported strftime formats : %U, %W, %C, %g, %r, %R, %T, %X, %c, %D, %F, %x * * @param string $dateFormat a date format * @return string */ public static function dateFormatToStrftime($dateFormat) { $caracs = array( // Day - no strf eq : S 'd' => '%d', 'D' => '%a', 'j' => '%e', 'l' => '%A', 'N' => '%u', 'w' => '%w', 'z' => '%j', // Week - no date eq : %U, %W 'W' => '%V', // Month - no strf eq : n, t 'F' => '%B', 'm' => '%m', 'M' => '%b', // Year - no strf eq : L; no date eq : %C, %g 'o' => '%G', 'Y' => '%Y', 'y' => '%y', // Time - no strf eq : B, G, u; no date eq : %r, %R, %T, %X 'a' => '%P', 'A' => '%p', 'g' => '%l', 'h' => '%I', 'H' => '%H', 'i' => '%M', 's' => '%S', // Timezone - no strf eq : e, I, P, Z 'O' => '%z', 'T' => '%Z', // Full Date / Time - no strf eq : c, r; no date eq : %c, %D, %F, %x 'U' => '%s' ); return strtr((string)$dateFormat, $caracs); } I use it with this condition on a date format : if(preg_match("/(D|l|F|M)/", $format)) http://php.net/manual/en/function.strftime.php

« previous php.notes (#166406) next »