English 中文(简体)
如果变数与购买力平价相符,则核对
原标题:Check if variable is a valid date with PHP

我正在用一个文字,从加拿大安全局的档案中进口一些数据。 在我这样做的时候,我想能够检查一个变数,看它是否有效。

我看到了几种检查方法,如果是假日,但大多数检查方式现在都要求你。 我不知道日期将采用的形式。

现在,我正在使用体力,但这不能轻易做到。

$field ="May";
if(strtotime($field)){
    echo "This is a date";
}

在该案中,“5月”是第一个名字的人,根本不是日期。

没有人会建议更可靠的职能?

www.un.org/spanish/ecosoc Edit based on questions from some of You.

就我而言,如果一个变数要作为“日期”来通过,则需要具体到一天/月/年,因此,仅仅“5月”就模糊不清。

基于这一点和下文所述的保尔斯良好观点,我们也能够检测到扼杀是否包含一些内容,例如:

$field ="May";
if(strtotime($field) && 1 === preg_match( ~[0-9]~ , $field)){
    echo "This is a date";
}else{
    echo "Nope not a date";
}

这似乎满足了我眼前的需要,但人们能否发现任何问题或提出改进建议?

问题回答

rel=“noreferer”>date_parse 并检查返回阵列的数值

$date = date_parse("May")

// ["year"] == FALSE
// ["month"] == 5
// ["day"] == FALSE

也可将这些内容通过checkdate

$date = date_parse($someString);
if ($date["error_count"] == 0 && checkdate($date["month"], $date["day"], $date["year"]))
    echo "Valid date";
else
    echo "Invalid date";

我不认为对这个问题有一个全方位的答案。 根据您的使用情况,你可能有不同的战略。

您的<代码>strtotime()是一个完美的解决办法,但正如你所说,你最终可能怀着虚假的正面态度。 为什么? 由于可<>> 然而,<代码>留待时间(5月)的结果如何?

echo date(DateTime::ISO8601, strtotime( May ));
2012-05-21T00:00:00+0200

因此,只给这个月,从一个月的午夜开始,将回到本年度和当日。 一种可能的解决办法是,检查你目前和(或)本年度的所作所为是否包含,这样,你就可以核对,确保你的日期具有完全的合格日期。

echo date(DateTime::ISO8601, strtotime( May Day )); // (strtotime() returns false)
1970-01-01T01:00:00+0100

echo date(DateTime::ISO8601, strtotime( May 21 ));
2012-05-21T00:00:00+0200

一种简单的<条码>,即,或者哪怕是ex。

However it is a bit odd and should be used only if you have no other way to do.

我认为,更好的解决办法是确定一套有效格式,并推断结果,以确保该日期有效。

$validDateFormatPatterns = array(
  [0-9]{1,2}-[0-9]{1,2}-[0-9]{4} , // 21-05-2012, obviously this pattern is simple and would accept 05-21-2012,
  the [0-9]{1,2}(th|st|nd|rd) (January|February|...|May|...|December) [0,9]{4} , // The 21st May 2012
);

You should try to cover most of the case and I m sure you will be able to find regex that checks for most current date format.

无论如何,你可能需要不时地调整你的职能,因为没有轻而易举的办法来防弹。

我很早就知道这一点,但我看着这一点,并试图避免reg。

function checkInputIsDate($date) {
    return (bool)strpbrk($date,1234567890) && strtotime($date);
}

This works because it takes away the issues posted above where only a month is passed into strtotime by making sure there are numbers in the string with strpbrk as well as verifying strtotime outputs a date.

而且,我知道的是,我知道有了一个职能。

希望有助于人们。





相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签