Reputation: 301
Is there a way on PHP I can check if I'm posting only a date without a time?
Ex.
2015-11-18 -> TRUE
2015-11-18 00:00:00 -> FALSE
2015-11-18 23:59:00 -> FALSE
Thank you!
Upvotes: 0
Views: 96
Reputation: 1
This can be done in a simple way .
What I have done is , I split the string into an array .
As all the 3 strings (Date sample given by you),differentiates from each other by :
symbol. So Split it and use count()
method.if its split then count()
will return value > 1
.That's it in a simple way..
<?php
//U can use $a=date("Y-m-d");
$a='2015-11-18'; //Here i am taking an Example
$b=explode(':' , $a);
if(count($b)>1)
{
echo "FALSE";
}else
{
echo "TRUE";
}
Hope You were expecting something like this .
Note- I have answered as per your question.For general case it will be different.
Upvotes: 0
Reputation: 1085
To get the current date you can use-
date("Y-m-d")
It gives only the date.
or if you already have a date -
$date = '10.21.2011';
echo date('Y-m-d', strtotime(str_replace('.', '/', $date)));
to validate you can use preg_match
-
$date="10.21.2011";
if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date))
{
return true;
}else{
return false;
}
$date = "2014-04-01 12:00:00";
preg_match('/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/',$date);
Upvotes: 1
Reputation: 43552
You can use my function (or on PHP.net) to validate date&time for this purpose:
var_dump( validateDate('2015-11-18', 'Y-m-d') ); # true
var_dump( validateDate('2015-11-18 00:00:00', 'Y-m-d') ); # false
var_dump( validateDate('2015-11-18 23:59:00', 'Y-m-d') ); # false
Upvotes: 0