Skip to content

Instantly share code, notes, and snippets.

@rajbdilip
Last active May 13, 2016 19:53
Show Gist options
  • Save rajbdilip/6664bd75e3a011023c9211b52d51880a to your computer and use it in GitHub Desktop.
Save rajbdilip/6664bd75e3a011023c9211b52d51880a to your computer and use it in GitHub Desktop.
Smarty Date Format to PHP Date Format converter
<?php
$arr = array(
'M' => '%b',
'F' => '%B',
'd' => '%d',
'j' => '%e',
'S' => '%f',
'H' => '%H',
'h' => '%I',
'G' => '%k',
'g' => '%l',
'm' => '%m',
'i' => '%M',
'a' => '%p',
'A' => '%P',
'Y' => '%Y'
);
$arr2 = array(
'%b' => 'M',
'%B' => 'F',
'%d' => 'd',
'%e' => 'j',
'%E' => 'j',
'%f' => 'S',
'%H' => 'H',
'%I' => 'h',
'%k' => 'G',
'%l' => 'g',
'%m' => 'm',
'%M' => 'i',
'%p' => 'a',
'%P' => 'A',
'%Y' => 'Y'
);
$cFormat = $sFormat = "";
if ( isset( $_GET['s'] ) && !empty( $_GET['s'] ) ) {
$sFormat = $_GET['s'];
$sFormatArray = str_split( $sFormat );
foreach ( $sFormatArray as $c ) {
if ( array_key_exists( $c, $arr ) )
$cFormat .= $arr[$c];
else
$cFormat .= $c;
}
} else if ( isset( $_GET['c'] ) && !empty( $_GET['c'] ) ) {
$cFormat = $_GET['c'];
$find = array_keys($arr2);
$replace = array_values($arr2);
$sFormat = str_replace( $find, $replace, $cFormat );
}
/*
* %b - M - abbreviated month name ('Jan'..'Dec')
* %B - F - full month name ('January'..'December')
* %d - d - day of the month as a decimal number, 0-padded ('01'..'31')
* %e - j - day of the month as a decimal number, blank-padded (' 1'..'31')
* %E - j - day of the month as a decimal number ('1'..'31')
* %f - S - English ordinal suffix for the day of the month ('st', 'nd', 'rd', 'th')
* %H - H - hour in 24-hour format, 0-padded ('00'..'23')
* %I - h - hour in 12-hour format, 0-padded ('01'..'12')
* %k - G - hour in 24-hour format, blank-padded (' 0'..'23')
* %l - g - hour in 12-hour format, blank-padded (' 1'..'12')
* %m - m - month as a decimal number, 0-padded ('01'..'12')
* %M - i - minute, 0-padded ('00'..'60')
* %p - a - lowercase ante/post meridiem ('am', 'pm')
* %P - A - uppercase ante/post meridiem ('AM', 'PM')
* %Y - Y - year as a decimal number including the century ('2005')
Y/m/D h:i A
*/
?>
<html>
<body>
<strong><?php echo date( $sFormat, time() ); ?></strong></body>
<form method="get" action="conv.php">
<input type="text" placeholder="Standard Format" name="s" value="<?php echo $sFormat; ?>" /><br/>
<input type="text" placeholder="Custom Format" name="c" value="<?php echo $cFormat; ?>"/><br/>
<input type="submit">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment