简体   繁体   English

php版的mysql的str_to_date()

[英]php version of mysql's str_to_date()

I've come across this in my companies code base, and I'd like to change it to something that doesn't connect to mysql just to just a function: 我在我的公司代码库中遇到过这个问题,我想把它改成只是函数而不能连接到mysql的东西:

$sql = "select date_format(str_to_date('$date','$format'),'%Y-%m-%d') startDate ";
$result = mysql_query($sql);
...

Our production env is of the unix variety, as is my dev environment, but some people write code on a windows environment, so strptime() isn't an option. 我们的生产环境属于unix种类,我的开发环境也是如此,但有些人在windows环境中编写代码,因此strptime()不是一种选择。

I've seen similar questions floating around SO, but none with an answer that fits my needs. 我已经看到类似的问题浮动SO,但没有一个答案符合我的需求。 Is there a simple, or common way to extract dates from strings using a variable format? 有一种简单或常用的方法可以使用变量格式从字符串中提取日期吗?

The reason the date formats can vary so much is because we're parsing file names from vendors, so we have to be able to handle yyyymmdd, mmddyyyy, ddmmyyyy, ddmonyyyy, etc, etc. 日期格式变化如此之大的原因是因为我们正在解析供应商的文件名,所以我们必须能够处理yyyymmdd,mmddyyyy,ddmmyyyy,ddmonyyyy等等。

There is the PHP function strtotime() PHP Ref which converts most strings which are a time (including things like "next Thursday") into timestamps, but even it can be somewhat confused when dealing with European and American date structures ("dd/mm/yyyy" versus "mm/dd/yyyy"). 有一个PHP函数strtotime() PHP Ref将大多数字符串(包括“下周四”之类的字符串)转换为时间戳,但即使在处理欧洲和美国日期结构时也会有些混淆(“dd / mm / yyyy“对比”mm / dd / yyyy“)。

You can test it out (without writing a test script) at http://www.functions-online.com/strtotime.html 您可以在http://www.functions-online.com/strtotime.html上测试它(无需编写测试脚本)

The short answer is that there is no way to automatically parse arbitrary date formats that will correctly parse all formats out there. 简短的回答是,没有办法自动解析任意日期格式,这将正确解析所有格式。 In your own example formats, there is just no way to know which numbers are for which date increments. 在您自己的示例格式中,没有办法知道哪个数字是哪个日期增量。

You can look at the accepted formats of strtotime() and if all your vendors use formats recognized by it, then you are in luck. 您可以查看strtotime()的可接受格式,如果您的所有供应商都使用它识别的格式,那么您很幸运。 But if not, the best you can really do is create a lookup table of "vendor" => "format" and then you can use date_parse_from_format() 但如果没有,你真正做的最好的是创建一个"vendor" => "format"的查找表,然后你可以使用date_parse_from_format()

edit: *based on comment below, here is a php4 version of an approximation of date_parse_from_format() that should suit your needs* 编辑: *根据下面的评论,这里是一个php4版本的date_parse_from_format()近似值,应该符合您的需求*

function date_parse_from_format($format, $date) {
  $dMask = array('H'=>'hour','i'=>'minute','s'=>'second','y'=>'year','m'=>'month','d'=>'day');
  $format = preg_split('//', $format, -1, PREG_SPLIT_NO_EMPTY);  
  $date = preg_split('//', $date, -1, PREG_SPLIT_NO_EMPTY);  
  foreach ($date as $k => $v) {
    if ($dMask[$format[$k]]) $dt[$dMask[$format[$k]]] .= $v;
  }
  return $dt;
}

Basically you need to have a lookup table of vendor => format where format is a mask of what each character in the date string represents. 基本上你需要有一个vendor => format的查找表,其中format是日期字符串中每个字符所代表的掩码。

NOTE: The masks used for each date/time increments do NOT exactly reflect what is used in php's normal date() string format. 注意:用于每个日期/时间增量的掩码并不完全反映php的普通日期()字符串格式中使用的掩码。 It is simplified and meant to mask each individual character of your custom string. 它被简化并意味着屏蔽自定义字符串的每个字符。

Example: 例:

/*
  lookup table for vendors
  for EVERY character you want to count as a date/time 
  increment you must use the following masks:
  hour   : H
  minute : i
  second : s
  year   : y
  month  : m
  day    : d
*/
$vendorDateFormats = array(
  'vendor1' => 'yyyymmdd',
  'vendor2' => 'mmddyyyy',
  'vendor3' => 'ddmmyyyy',
  'vendor4' => 'yyyy.mm.dd HH:ii:ss'
);

// example 1:
echo "<pre>";
print_r(date_parse_from_format($vendorDateFormats['vendor2'],'03232011'));

// example 2:
echo "<pre>";
print_r(date_parse_from_format($vendorDateFormats['vendor4'],'2011.03.23 12:03:00'));

output: 输出:

Array
(
    [month] => 03
    [day] => 23
    [year] => 2011
)

Array
(
    [year] => 2011
    [month] => 03
    [day] => 23
    [hour] => 12
    [minute] => 03
    [second] => 00
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM