This question already has answers here:
date validation not to be today
(3 answers)
How can I compare two dates in PHP?
(13 answers)
Closed 7 years ago.
I have used a php code to validate two dates.If date 1 = date 2 then return false els true.Below is the code where error is it is always returning false.
{
$d1 =$values['DATE']; //date 1 in dd-mm-yyyy format
$d2 =$values['DATE_OF_LEAVE']; //date 2 in dd-mm-yyyy format
if(strtotime($d1) != strtotime($d2))
{
return true;
}
echo '<script type="text/javascript">';
echo 'alert("Application date and leave date should be different.")';
echo '</script>';
return false;
}
Set both to a least common denominator with date().
if(date('Ymd',strtotime($d1)) != date('Ymd',strtotime($d2)))
Related
This question already has answers here:
DateTime converts wrong when system time is 30 March
(2 answers)
Closed 1 year ago.
I want to convert this date 2021-02 into 02-2021, so I did:
echo DateTime::createFromFormat('Y-m', '2021-02')->format('m/Y');
but this return 03/2021
and the correct result is 02/2021
you have to specify the day:
echo DateTime::createFromFormat('Y-m-d', '2021-02-someday')->format('m/Y');
for example:
echo DateTime::createFromFormat('Y-m-d', '2021-02-01')->format('m/Y');
This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 5 years ago.
i need to compare two date from string,
my dates:
first date: 11-11-19
second date: 11-24-17
so i try to
$firstdate = "11-11-19";
$seconddate = "11-24-17";
if($firstdate < $seconddate)
{
echo "firstdate is minor than the secondate";
}
else
{
echo "seconddate is major than the firstdate";
}
if i change < or > the if statement should change, but i get always the firsdate...
how to do to compare two dates in this forma mm-dd-yy ?
Thanks
You can use strtotime to convert string to unix timestamp, and compare that.
$firstdate = strtotime("11-11-19");
$seconddate = strtotime("11-24-17");
if($firstdate < $seconddate)
{
echo "firstdate is minor than the secondate";
}
else
{
echo "seconddate is major than the firstdate";
}
You can convert both dates to timestamp:
echo (strtotime($firstdate) < strtotime($seconddate)) ? "firstdate is minor than the secondate" : "seconddate is major than the firstdate";
This question already has answers here:
Check if two PHP datetime objects are set to the same date ( ignoring time )
(10 answers)
Closed 7 years ago.
Here is my question:
I have 2 datetime fields with the following values.
- n_date = '2015-11-06 00:00:00.000'
- ArrivalDate = '2015-11-06 01:00:00.000'
I want to compare only the date part of these fields so that the following:
if ($n_date!=$v[ArrivalDate]
{
do something ...
}
Does not do something
thx in advance!
Use strtotime() for this,
if((date("Y-m-d",strtotime($n_date))) == (date("Y-m-d",strtotime($ArrivalDate)))) {
// both dtae are equal
} else {
// both date are not equal
}
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I would like to convert 30.10.2014 15\:25\:24 to 30/Oct/2014 15:25:24. I tried it like this...
$s = '30.10.2014 15\:25\:24';
$date = strtotime($s);
echo date('d/M/Y H:i:s', $date);
... but my result is 01.Jan.1970 01:00:00
Its because strtotime returned FALSE , code shown below
$s = '30.10.2014 15\:25\:24'; //invalid format
$date = strtotime($s);
if($date===FALSE) //if strtotime failed
{
echo "strtotime failed";
}
This question already has answers here:
How to compare two dates in php [duplicate]
(16 answers)
Closed 8 years ago.
How can I check if a date captured in the database is less than the current date?
foreach( $results as $r ) {
$date = $r['date'];
if($date < $currentdate) {
echo 'oldest dates';
} else {
echo 'newest dates';
}
Use strtotime function:
$date = strtotime($date);
$currentdate = strtotime($currentdate);
Use strtotime($date)<strtotime($currentdate)
and make sure that both dates are in correct format.