note 74366 deleted from function.strtotime by bjori
| From: | bjori@php.net | Date: | Sun, 08 Apr 2007 08:08:27 +0000 |
| Subject: | note 74366 deleted from function.strtotime by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125185@lists.php.net to get a copy of this message | ||
Note Submitter: PHP.SU
Reason: evil code
----
strtotime() function use GNU format.
You can convert date to unix timestamp by using all date formats!
Use follow parse_date() function:
<?php
function convert_date($date, $replace_separator = FALSE)
{
$days = '0?[1-9]|[12][0-9]|3[01]';
$months= '0?[1-9]|1[0-2]';
$year = '\d{2}|\d{4}';
$non_alpha = '[^0-9a-zA-Z]+';
return preg_replace( "/^\s*($days)($non_alpha)($months)($non_alpha)($year)/",
$replace_separator === FALSE ? '$3$2$1$4$5' :
'$3'.$replace_separator.'$1'.$replace_separator.'$5', $date);
}
function parse_date($date)
{
$date = str_replace("-", "/", $date);
$date = str_replace(".", "/", $date);
$predate = date("d.m.Y", strtotime(convert_date($date, '/')));
$reformatted = preg_replace("/^\s*([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\.
-]+([0-9]{1,4})/", "\\2/\\1/\\3", $predate);
$rt_time = strtotime($reformatted);
if ($rt_time<=0) return 0;
else return $rt_time;
}
?>
Example:
<?php
echo parse_date("26.01.1993");
echo parse_date("01-26-1993");
echo parse_date("26-01-1993");
echo parse_date("26/01/1993");
echo parse_date("1993-01-26");
echo parse_date("01/26/1993");
// all will take unix timestamp: 727988400
// very good!
?>
Use parse_date() and forget your troubles ;-)