PHP Date validation using DateTime

DateTime and DateInterval classes are part of the PHP standard library. It’s better to perform such validation tasks using base library classes.

$fromdate = ‘2011-08-18’;
$todate = ‘2011-08-17’;
$date1 = new DateTime($fromdate);
$date2 = new DateTime($todate);
$diff = $date1->diff($date2)->format(‘%R%a’);
if (0 > $diff)
echo ‘From date should be less than or equal to To date.’;

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Advertisements
%d bloggers like this: