Date validation with input type date

时间:2015-06-27 02:58:18

标签: php

I have two types of date, I need to check the end date should greater than start date.

These are two input:

<input id="startdate" type="date" name="startdate" />

<input id="enddate" type="date" name="enddate" />
<div class="errmsg" id="errmsg"></div>

I need if the end date less than start date (show error message in errmsg div)

5 个答案:

答案 0 :(得分:2)

$startdate = strtotime($_POST['startdate']);
$enddate = strtotime($_POST['enddate']);

if ($enddate < $startdate) {
$error = 'Error!';
}

echo $error;

答案 1 :(得分:1)

您可以将strtotime用于html5输入日期类型

<?php
    $startdate= $_POST['startdate'];
    $enddate= $_POST['enddate'];

    $start = strtotime($startdate);
    $end = strtotime($enddate);
    if($end < $start){
        //show error
    }
?>

答案 2 :(得分:0)

这里有一个建议:

首先,您的输入可以位于以下格式:

<form action='action_code.php' method="post">
     <input id="startdate" type="date" name="startdate"  /> 
     <input id="enddate" type="date" name="enddate" />
     <input type="submit" value="Submit"> 
</form>

并且action_code.php文件将包含:

<?php

$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];

if($enddate < $startdate){
    echo 'enddate has to be after startdate';
}else{
    echo 'form submitted';
    echo $startdate;
    echo $enddate;
}    

echo '<a href="/home.php">Back home</a>';
?>

这里要保留的是,可以使用比较运算符同样比较日期。我省略了任何格式以保持代码清洁。

答案 3 :(得分:0)

您可以使用checkdate ()进行日期验证checkdate php.net

$date = $_POST['enddate'];
$test_date = explode('/', $date);
if (count($test_date) == 3)
{
    if (checkdate($test_date[0], $test_date[1], $test_date[2])) {
        //  date is  valid
    }
    else
    {
        // Invalid date dates
    }
} else
{
    // invalid input
}

并比较两个日期

$today_dt = strtotime($_POST['startdate']);
$expire_dt = strtotime($_POST['enddate']);

if ($expire_dt < $today_dt)
{
    /* Do something */
}

Refer this answer

答案 4 :(得分:-1)

使用jquery作为

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ASZ 5

int  check_uniq (int a[][ASZ], size_t n, int v);
void prn_array (int a[][ASZ], size_t n);
void prn_array_1d (int a[][ASZ], size_t n);
void quick_sort (int *a, size_t n);

int main (void) {

    int a[][ASZ] = {{19, 1,38,51,37}, 
                    {95,74,42,23,76},
                    {17,24,14,22,25},
                    {11,50,10,84,45},
                    {78,44,66,46,98}};
    int b[ASZ][ASZ] = During Sort;
    int tmp = 0;
    size_t i, j;
    const size_t n = sizeof a/sizeof *a;

    srand (time (NULL));

    /* fill second array with values unique to values in a[][] */
    for (i = 0; i < n; i++) {
        for (j = 0; j < ASZ; j++) {
            for (;;) {
                tmp = rand() % 100 + 1;
                if (check_uniq (a, n, tmp))
                    break;
            }
            b[i][j] = tmp;
        }
    }

    printf ("\n the original array is:\n\n");
    prn_array (a, n);

    printf ("\n the second array with unique values is:\n\n");
    prn_array (b, n);

    printf ("\n comparison in sorted 1D array format (easier to check):\n\n");
    prn_array_1d (a, n);
    printf ("\n");
    prn_array_1d (b, n);
    printf ("\n\n");

    return 0;
}

int check_uniq (int a[][ASZ], size_t n, int v)
{
    size_t i, j;

    for (i = 0; i < n; i++)
        for (j = 0; j < ASZ; j++)
            if (a[i][j] == v)
                return 0;

    return 1;
}

void prn_array (int a[][ASZ], size_t n)
{
    size_t i, j;
    for (i = 0; i < n; i++) {
        for (j = 0; j < ASZ; j++)
            printf (" %3d", a[i][j]);
        printf ("\n");
    }
}

void prn_array_1d (int a[][ASZ], size_t n)
{
    size_t i, j;
    int tmp [n * ASZ];

    for (i = 0; i < n; i++)
        for (j = 0; j < ASZ; j++)
            tmp [i * ASZ + j] = a[i][j];

    quick_sort (tmp, n * ASZ);

    for (i = 0; i < n * ASZ; i++)
        printf (" %3d", tmp[i]);
}

void quick_sort (int *a, size_t n)
{
    int pvt, tmp;
    size_t i, j;

    if (n < 2) return;

    pvt = a[n / 2];
    for (i = 0, j = n - 1;; i++, j--) 
    {
        while (a[i] < pvt) i++;
        while (pvt < a[j]) j--;

        if (i >= j)
            break;

        tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }

    quick_sort (a, i);
    quick_sort (a + i, n - i);
}
相关问题