note 46947 added to function.date
| From: | mihai_axe at osu1 dot php dot net | Date: | Thu, 28 Oct 2004 13:47:34 +0000 |
| Subject: | note 46947 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-79251@lists.php.net to get a copy of this message | ||
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>
';
?>
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46947
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46947&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46947&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46947&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46947&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46947&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46947&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46947&report=yes
Search -- http://master.php.net/manage/user-notes.php