找到你要的答案

Q:Check date format before changing it with PHP

Q:PHP改变它之前检查日期格式

I am looking for a way to check a date format before changing the format. My system allows users to upload spreadsheets of data, and one of this fields is date of birth.

This biggest issue I have run into this so far is mm/dd/yy vs mm/dd/yyyy. I have found an example of fixing this on PHP convert 2 digit year to a 4 digit year however I don't always want to do this.

Is there a way I can check the PHP Date format in an If statement? I don't want to rely on counting as 1/1/1973 is the same amount of digits as 01/01/73, but the year issue would get messed up still.

Anyone have any ideas on how I can check dateformat before manipulating it.

在更改格式之前,我正在寻找一种检查日期格式的方法。我的系统允许用户上传数据表,其中一个字段是出生日期。

这个最大的问题,我遇到了这个到目前为止是年/月/日VS MM/dd/yyyy。我发现一例固定这个PHP转换2位年4位数的年但是我总是不想做这个。

有一个方法可以检查PHP日期格式在一个if语句?我不想指望计数为1 / 1 / 1973是相同数量的数字为01 / 01 / 73,但今年的问题会搞砸了。

任何人有任何想法如何我可以操作前检查日期格式。

answer1: 回答1:

Try using the DateTime class. The default constructor can interpret your date strings. This should void your need for conditional checks.

$date1 = "1/1/1973";
$date2 = "01/01/73";

$dt1 = new DateTime($date1);
$dt2 = new DateTime($date2);

echo $dt1->format("m/d/Y");     // prints as 01/01/1973
echo $dt2->format("m/d/Y");     // prints as 01/01/1973

EDIT

For two digit years below 1970, you can try this, but it will work if and only if your current and future data is entered as four digit years. Otherwise people born between 2003 and 2069 will have their birthdays converted to 19xx.

Note: We're using 2003 because the OP indicated that all new entries will be forced to four digit years, and (at the time of posting) no one under 13 will be using the software.

$format = "Y";

if(!is_numeric(substr($date, -4)))      // Checks for 2 digit year
{
    $yy = substr($date, -2);            // Gets 2 digit year
    $format = "y";

    if($yy < 70 && $yy > 2)             // Looking for 1903 to 1969 
    {
        $yy += 1900;

        // Rebuild the date string with 4 digit year
        $date = substr($date, 0, strlen($date) - 2) . $yy;
        $format = "Y";
    }
}

$delimiters = array("/", "-", ".");     // Different date delimiters to check

foreach($delimiters as $delim)
{
    if(strpos($date, $delim) !== false)
    {
        $dt = DateTime::createFromFormat("m" . $delim . "d" . $delim . $format, $date);
    }
}

echo $dt->format("m/d/Y");

尝试使用DateTime类。默认构造函数可以解释日期字符串。这将减少您的条件检查的需要。

$date1 = "1/1/1973";
$date2 = "01/01/73";

$dt1 = new DateTime($date1);
$dt2 = new DateTime($date2);

echo $dt1->format("m/d/Y");     // prints as 01/01/1973
echo $dt2->format("m/d/Y");     // prints as 01/01/1973

编辑

对于两位数低于1970年,您可以尝试这一点,但它会工作,当且仅当您当前和未来的数据输入为四位数年。否则人们出生于2003和2069之间将有他们的生日转换为19xx。

注:我们使用2003,因为OP表明,所有新的条目将被迫到四位数的年,(在张贴的时候)没有人在13岁以下将使用该软件。

$format = "Y";

if(!is_numeric(substr($date, -4)))      // Checks for 2 digit year
{
    $yy = substr($date, -2);            // Gets 2 digit year
    $format = "y";

    if($yy < 70 && $yy > 2)             // Looking for 1903 to 1969 
    {
        $yy += 1900;

        // Rebuild the date string with 4 digit year
        $date = substr($date, 0, strlen($date) - 2) . $yy;
        $format = "Y";
    }
}

$delimiters = array("/", "-", ".");     // Different date delimiters to check

foreach($delimiters as $delim)
{
    if(strpos($date, $delim) !== false)
    {
        $dt = DateTime::createFromFormat("m" . $delim . "d" . $delim . $format, $date);
    }
}

echo $dt->format("m/d/Y");
answer2: 回答2:

Is it then possible for me to include this code in my SQL insert command? '$date = '01/24/2006'; $date = date('Y-m-d', strtotime($date)); $sql = "INSERT INTO table SET date = '$date'";

Is it then possible for me to include this code in my SQL insert command? '$date = '01/24/2006'; $date = date('Y-m-d', strtotime($date)); $sql = "INSERT INTO table SET date = '$date'";

php  date