note 100102 deleted from function.date by tularis
| From: | tularis@php.net | Date: | Thu, 09 Feb 2012 20:54:12 +0000 |
| Subject: | note 100102 deleted from function.date by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185973@lists.php.net to get a copy of this message | ||
Note Submitter: root dot packet at gmail dot com
----
If you are sick of writing lots of code to format dates correctly, then I hope you find my code
useful. It can def. use some work, I plan to improve it, but it passed a boring night and gave good
results. enjoy! God bless!
<?php
function datefmt($date, $inFormat, $outFormat) {
/* A function to take a date in ($date) in specified inbound format (eg mm/dd/yy for 12/08/10) and
* return date in $outFormat (eg yyyymmdd for 20101208)
* datefmt (
* string $date - String containing the literal date that will be modified
* string $inFormat - String containing the format $date is in (eg. mm-dd-yyyy)
* string $outFormat - String containing the desired date output, format the same as date()
* )
*
*
* ToDo:
* - Add some error checking and the sort?
*/
$order = array('mon' => NULL, 'day' => NULL, 'year' =>
NULL);
for ($i=0; $i<strlen($inFormat);$i++) {
switch ($inFormat[$i]) {
case "m":
$order['mon'] .= substr($date, $i, 1);
break;
case "d":
$order['day'] .= substr($date, $i, 1);
break;
case "y":
$order['year'] .= substr($date, $i, 1);
break;
}
}
$unixtime = mktime(0, 0, 0, $order['mon'], $order['day'],
$order['year']);
$outDate = date($outFormat, $unixtime);
if ($outDate == False) {
return False;
} else {
return $outDate;
}
}
?>