Speed up development with full-stack environments for every branch.

Learn More

Check a date validity with checkdate() function [PHP]

346 Runs 3371 Views 508 Copies
Saved

Saved

sundip 66

sundip
published 4 years ago

<?php

/**
 * Check date validity (validates a Gregorian date) with checkdate()
 * 
 */
$dates = array('20,10,2012', '10,10,2010', '13,12,2012');
echo '<center>';
foreach ($dates as $date) {
    //exploding each date to separate the values
    //$d[0] will be the month value, $d[1] the day value and $d[2] the year value
    $d = explode(',', $date);
    //checkdate accepts 3 integer parameters month,day,year 
    //(that's why we separated the date into pieces before)
    if (checkdate($d[0], $d[1], $d[2]))
        echo '<br /><div style="background-color:green;color:#fff;padding:10px;width:600px;font-size:16px">
        <b>The date with month=' . $d[0] . ', day=' . $d[1] . ', year=' . $d[2] . ' is a valid date</b>
        </div><br />';
    else
        echo '<br /><div style="background-color:red;color:#fff;padding:10px;width:600px;font-size:16px">
        <b>The date with month=' . $d[0] . ', day=' . $d[1] . ', year=' . $d[2] . ' is an invalid date</b>
        </div><br />';
}

echo '</center>';
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).