This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 1 year ago.
How to compare two dates in php if dates are in format '03_01_12' and '31_12_11' .
I am using this code:
$date1=date('d_m_y');
$date2='31_12_11';
if(strtotime($date1) < strtotime($date2))
echo '1 is small ='.strtotime($date1).','.$date1;
else
echo '2 is small ='.strtotime($date2).','.$date2;
But its not working..
You will have to make sure that your dates are valid date objects.
Try this:
$date1=date('d/m/y');
$tempArr=explode('_', '31_12_11');
$date2 = date("d/m/y", mktime(0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]));
You can then perform the strtotime() method to get the difference.
Using DateTime::createFromFormat:
$format = "d_m_y";
$date1 = \DateTime::createFromFormat($format, "03_01_12");
$date2 = \DateTime::createFromFormat($format, "31_12_11");
var_dump($date1 > $date2);
Not answering the OPs actual problem, but answering just the title. Since this is the top result for "comparing dates in php".
Pretty simple to use Datetime Objects (php >= 5.3.0) and Compare them directly
$date1 = new DateTime("2009-10-11");
$date2 = new DateTime("tomorrow"); // Can use date/string just like strtotime.
var_dump($date1 < $date2);
The date_diff() function returns the difference between two DateTime objects.
If the first date is before the second date a positive number of days will be returned; otherwise a negative number of days:
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
output will be "+272 days" ;
changing
$date1 = "2014-03-15"
<?php
$date1=date_create("2014-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
Output will be "-93 days"
<?php
$expiry_date = "2017-12-31 00:00:00"
$today = date('d-m-Y',time());
$exp = date('d-m-Y',strtotime($expiry_date));
$expDate = date_create($exp);
$todayDate = date_create($today);
$diff = date_diff($todayDate, $expDate);
if($diff->format("%R%a")>0){
echo "active";
}else{
echo "inactive";
}
echo "Remaining Days ".$diff->format("%R%a days");
?>
Extending #nevermind's answer, one can use DateTime::createFromFormat: like,
// use - instead of _. replace _ by - if needed.
$format = "d-m-y";
$date1 = DateTime::createFromFormat($format, date('d-m-y'));
$date2 = DateTime::createFromFormat($format, str_replace("_", "-",$date2));
var_dump($date1 > $date2);
you can try something like:
$date1 = date_create('2014-1-23'); // format of yyyy-mm-dd
$date2 = date_create('2014-2-3'); // format of yyyy-mm-dd
$dateDiff = date_diff($date1, $date2);
var_dump($dateDiff);
You can then access the difference in days like this $dateDiff->d;
Try this
$data1 = strtotime(\date("d/m/Y"));
$data1 = date_create($data1);
$data2 = date_create("21/06/2017");
if($data1 < $data2){
return "The most current date is date1";
}
return "The most current date is date2";
You can to converte for integer number and compare.
Eg.:
$date_1 = date('Ymd');
$date_2 = '31_12_2011';
$date_2 = (int) implode(array_reverse(explode("_", $date_2)));
echo ($date_1 < $date_2) ? '$date_2 is bigger then $date_1' : '$date_2 is smaller than $date_1';
benchmark comparison
date_create, strtotime, DateTime, and direct
direct is faster
$f1="2014-12-12";
$f2="2014-12-13";
$diff=false;
$this->bench('a');
for ($i=0; $i < 20000; $i++) {
$date1=date_create($f1);
$date2=date_create($f2);
$diff=date_diff($date1,$date2);
}
$this->bench('a','b');
for ($i=0; $i < 20000; $i++) {
$date1=strtotime($f1);
$date2=strtotime($f2);
if ($date1>$date2) {
$diff=true;
}
}
$this->bench('b','c');
for ($i=0; $i < 20000; $i++) {
$date1 = new DateTime($f1);
$date2 = new DateTime($f2);
if ($date1>$date2) {
$diff=true;
}
}
$diff=false;
$this->bench('c','d');
for ($i=0; $i < 20000; $i++) {
if ($f1>$f2) {
$diff=true;
}
}
$this->bench('d','e');
var_dump($diff);
$this->dumpr($this->benchs);
results:
[a] => 1610415241.4687
[b] => 1610415242.8759
[a-b] => 1.407194852829
[c] => 1610415243.5672
[b-c] => 0.69137716293335
[d] => 1610415244.7036
[c-d] => 1.1363649368286
[e] => 1610415244.7109
[d-e] => 0.0073208808898926
compare the result of maketime() for each of the time
I know this is late, but for future reference, put the date format into a recognised format by using str_replace then your function will work. (replace the underscore with a dash)
//change the format to dashes instead of underscores, then get the timestamp
$date1 = strtotime(str_replace("_", "-",$date1));
$date2 = strtotime(str_replace("_", "-",$date2));
//compare the dates
if($date1 < $date2){
//convert the date back to underscore format if needed when printing it out.
echo '1 is small='.$date1.','.date('d_m_y',$date1);
}else{
echo '2 is small='.$date2.','.date('d_m_y',$date2);
}
Don't know what your problem is but:
function date_compare($d1, $d2)
{
$d1 = explode('_', $d1);
$d2 = explode('_', $d2);
$d1 = array_reverse($d1);
$d2 = array_reverse($d2);
if (strtotime(implode('-', $d1)) > strtotime(implode('-', $d2)))
{
return $d2;
}
else
{
return $d1;
}
}
I think this one is very simple function
function terminateOrNotStringtoDate($currentDate, $terminationdate)
{
$crtDate = new DateTime($currentDate);
$termDate = new DateTime($terminationdate);
if($crtDate >= $termDate)
{
return true;
} else {
return false;
}
}
Guys Please don't make it so complex The simple answer bellow
$date1=date('d_m_y');
$date2='31_12_11';
$date1=str_replace('_', '-', $date1);
$date2=str_replace('_', '-', $date2)
if(strtotime($date1) < strtotime($date2))
echo '1 is small ='.strtotime($date1).','.$date1;
else
echo '2 is small ='.strtotime($date2).','.$date2;
I just have added two more lines with your code
If both dates are in the same format then use a comparison operator.
$date1 = "2018-05-05";
$date2 = "2019-08-19";
//comparison operator to
if ($date1 > $date2) {
echo "$date1 is latest than $date2";
}
else{
echo "$date1 is older than $date2";
}
Output:
2018-05-05 is older than 2019-08-19
Related
How can I compare two dates in PHP?
The date is stored in the database in the following format
2011-10-2
If I wanted to compare today's date against the date in the database to see which one is greater, how would I do it?
I tried this,
$today = date("Y-m-d");
$expire = $row->expireDate //from db
if($today < $expireDate) { //do something; }
but it doesn't really work that way. What's another way of doing it?
If all your dates are posterior to the 1st of January of 1970, you could use something like:
$today = date("Y-m-d");
$expire = $row->expireDate; //from database
$today_time = strtotime($today);
$expire_time = strtotime($expire);
if ($expire_time < $today_time) { /* do Something */ }
If you are using PHP 5 >= 5.2.0, you could use the DateTime class:
$today_dt = new DateTime($today);
$expire_dt = new DateTime($expire);
if ($expire_dt < $today_dt) { /* Do something */ }
Or something along these lines.
in the database the date looks like this 2011-10-2
Store it in YYYY-MM-DD and then string comparison will work because '1' > '0', etc.
Just to compliment the already given answers, see the following example:
$today = new DateTime('');
$expireDate = new DateTime($row->expireDate); //from database
if($today->format("Y-m-d") < $expireDate->format("Y-m-d")) {
//do something;
}
Update:
Or simple use old-school date() function:
if(date('Y-m-d') < date('Y-m-d', strtotime($expire_date))){
//echo not yet expired!
}
I would'nt do this with PHP.
A database should know, what day is today.( use MySQL->NOW() for example ), so it will be very easy to compare within the Query and return the result, without any problems depending on the used Date-Types
SELECT IF(expireDate < NOW(),TRUE,FALSE) as isExpired FROM tableName
$today = date('Y-m-d');//Y-m-d H:i:s
$expireDate = new DateTime($row->expireDate);// From db
$date1=date_create($today);
$date2=date_create($expireDate->format('Y-m-d'));
$diff=date_diff($date1,$date2);
//echo $timeDiff;
if($diff->days >= 30){
echo "Expired.";
}else{
echo "Not expired.";
}
Here's a way on how to get the difference between two dates in minutes.
// set dates
$date_compare1= date("d-m-Y h:i:s a", strtotime($date1));
// date now
$date_compare2= date("d-m-Y h:i:s a", strtotime($date2));
// calculate the difference
$difference = strtotime($date_compare1) - strtotime($date_compare2);
$difference_in_minutes = $difference / 60;
echo $difference_in_minutes;
You can convert the dates into UNIX timestamps and compare the difference between them in seconds.
$today_date=date("Y-m-d");
$entered_date=$_POST['date'];
$dateTimestamp1 = strtotime($today_date);
$dateTimestamp2 = strtotime($entered_date);
$diff= $dateTimestamp1-$dateTimestamp2;
//echo $diff;
if ($diff<=0)
{
echo "Enter a valid date";
}
I had that problem too and I solve it by:
$today = date("Ymd");
$expire = str_replace('-', '', $row->expireDate); //from db
if(($today - $expire) > $NUMBER_OF_DAYS)
{
//do something;
}
Here's my spin on how to get the difference in days between two dates with PHP.
Note the use of '!' in the format to discard the time part of the dates, thanks to info from DateTime createFromFormat without time.
$today = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
$wanted = DateTime::createFromFormat('!d-m-Y', $row["WANTED_DELIVERY_DATE"]);
$diff = $today->diff($wanted);
$days = $diff->days;
if (($diff->invert) != 0) $days = -1 * $days;
$overdue = (($days < 0) ? true : false);
print "<!-- (".(($days > 0) ? '+' : '').($days).") -->\n";
Found the answer on a blog and it's as simple as:
strtotime(date("Y"."-01-01")) -strtotime($newdate))/86400
And you'll get the days between the 2 dates.
This works because of PHP's string comparison logic. Simply you can check...
if ($startdate < $date) {// do something}
if ($startdate > $date) {// do something}
Both dates must be in the same format. Digits need to be zero-padded to the left and ordered from most significant to least significant. Y-m-d and Y-m-d H:i:s satisfy these conditions.
If you want a date ($date) to get expired in some interval for example a token expiration date when performing a password reset, here's how you can do:
$date = $row->expireDate;
$date->add(new DateInterval('PT24H')); // adds 24 hours
$now = new \DateTime();
if($now < $date) { /* expired after 24 hours */ }
But in your case you could do the comparison just as the following:
$today = new DateTime('Y-m-d');
$date = $row->expireDate;
if($today < $date) { /* do something */ }
first of all, try to give the format you want to the current date time of your server:
Obtain current date time
$current_date = getdate();
Separate date and time to manage them as you wish:
$current_date_only = $current_date[year].'-'.$current_date[mon].'-'.$current_date[mday];
$current_time_only = $current_date['hours'].':'.$current_date['minutes'].':'.$current_date['seconds'];
Compare it depending if you are using donly date or datetime in your DB:
$today = $current_date_only.' '.$current_time_only;
or
$today = $current_date_only;
if($today < $expireDate)
hope it helps
I Need To Compare Two Date Time Variable in Php for my project what is the best way to compare
$date1 = "2018-02-06 15:09:44";
$date2 = "2018-02-06 16:09:44";
Expected Result:
I need ans as $date2;
Thanks in advance
$date1 = new DateTime("2018-02-06 15:09:44");
$date2 = new DateTime("2018-02-06 16:09:44");
if($date1 > $date2 ) {
// statement
}else {
// other statement
}
$date1 = "2018-02-06 15:09:44";
$date2 = "2018-02-06 16:09:44";
if(strtotime($date1) < strtotime($date2)) {
echo "date1 less than date2";
} else {
echo "date1 is greater than date2;
}
This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 1 year ago.
How to compare two dates in php if dates are in format '03_01_12' and '31_12_11' .
I am using this code:
$date1=date('d_m_y');
$date2='31_12_11';
if(strtotime($date1) < strtotime($date2))
echo '1 is small ='.strtotime($date1).','.$date1;
else
echo '2 is small ='.strtotime($date2).','.$date2;
But its not working..
You will have to make sure that your dates are valid date objects.
Try this:
$date1=date('d/m/y');
$tempArr=explode('_', '31_12_11');
$date2 = date("d/m/y", mktime(0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]));
You can then perform the strtotime() method to get the difference.
Using DateTime::createFromFormat:
$format = "d_m_y";
$date1 = \DateTime::createFromFormat($format, "03_01_12");
$date2 = \DateTime::createFromFormat($format, "31_12_11");
var_dump($date1 > $date2);
Not answering the OPs actual problem, but answering just the title. Since this is the top result for "comparing dates in php".
Pretty simple to use Datetime Objects (php >= 5.3.0) and Compare them directly
$date1 = new DateTime("2009-10-11");
$date2 = new DateTime("tomorrow"); // Can use date/string just like strtotime.
var_dump($date1 < $date2);
The date_diff() function returns the difference between two DateTime objects.
If the first date is before the second date a positive number of days will be returned; otherwise a negative number of days:
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
output will be "+272 days" ;
changing
$date1 = "2014-03-15"
<?php
$date1=date_create("2014-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
Output will be "-93 days"
<?php
$expiry_date = "2017-12-31 00:00:00"
$today = date('d-m-Y',time());
$exp = date('d-m-Y',strtotime($expiry_date));
$expDate = date_create($exp);
$todayDate = date_create($today);
$diff = date_diff($todayDate, $expDate);
if($diff->format("%R%a")>0){
echo "active";
}else{
echo "inactive";
}
echo "Remaining Days ".$diff->format("%R%a days");
?>
Extending #nevermind's answer, one can use DateTime::createFromFormat: like,
// use - instead of _. replace _ by - if needed.
$format = "d-m-y";
$date1 = DateTime::createFromFormat($format, date('d-m-y'));
$date2 = DateTime::createFromFormat($format, str_replace("_", "-",$date2));
var_dump($date1 > $date2);
you can try something like:
$date1 = date_create('2014-1-23'); // format of yyyy-mm-dd
$date2 = date_create('2014-2-3'); // format of yyyy-mm-dd
$dateDiff = date_diff($date1, $date2);
var_dump($dateDiff);
You can then access the difference in days like this $dateDiff->d;
Try this
$data1 = strtotime(\date("d/m/Y"));
$data1 = date_create($data1);
$data2 = date_create("21/06/2017");
if($data1 < $data2){
return "The most current date is date1";
}
return "The most current date is date2";
You can to converte for integer number and compare.
Eg.:
$date_1 = date('Ymd');
$date_2 = '31_12_2011';
$date_2 = (int) implode(array_reverse(explode("_", $date_2)));
echo ($date_1 < $date_2) ? '$date_2 is bigger then $date_1' : '$date_2 is smaller than $date_1';
benchmark comparison
date_create, strtotime, DateTime, and direct
direct is faster
$f1="2014-12-12";
$f2="2014-12-13";
$diff=false;
$this->bench('a');
for ($i=0; $i < 20000; $i++) {
$date1=date_create($f1);
$date2=date_create($f2);
$diff=date_diff($date1,$date2);
}
$this->bench('a','b');
for ($i=0; $i < 20000; $i++) {
$date1=strtotime($f1);
$date2=strtotime($f2);
if ($date1>$date2) {
$diff=true;
}
}
$this->bench('b','c');
for ($i=0; $i < 20000; $i++) {
$date1 = new DateTime($f1);
$date2 = new DateTime($f2);
if ($date1>$date2) {
$diff=true;
}
}
$diff=false;
$this->bench('c','d');
for ($i=0; $i < 20000; $i++) {
if ($f1>$f2) {
$diff=true;
}
}
$this->bench('d','e');
var_dump($diff);
$this->dumpr($this->benchs);
results:
[a] => 1610415241.4687
[b] => 1610415242.8759
[a-b] => 1.407194852829
[c] => 1610415243.5672
[b-c] => 0.69137716293335
[d] => 1610415244.7036
[c-d] => 1.1363649368286
[e] => 1610415244.7109
[d-e] => 0.0073208808898926
compare the result of maketime() for each of the time
I know this is late, but for future reference, put the date format into a recognised format by using str_replace then your function will work. (replace the underscore with a dash)
//change the format to dashes instead of underscores, then get the timestamp
$date1 = strtotime(str_replace("_", "-",$date1));
$date2 = strtotime(str_replace("_", "-",$date2));
//compare the dates
if($date1 < $date2){
//convert the date back to underscore format if needed when printing it out.
echo '1 is small='.$date1.','.date('d_m_y',$date1);
}else{
echo '2 is small='.$date2.','.date('d_m_y',$date2);
}
Don't know what your problem is but:
function date_compare($d1, $d2)
{
$d1 = explode('_', $d1);
$d2 = explode('_', $d2);
$d1 = array_reverse($d1);
$d2 = array_reverse($d2);
if (strtotime(implode('-', $d1)) > strtotime(implode('-', $d2)))
{
return $d2;
}
else
{
return $d1;
}
}
I think this one is very simple function
function terminateOrNotStringtoDate($currentDate, $terminationdate)
{
$crtDate = new DateTime($currentDate);
$termDate = new DateTime($terminationdate);
if($crtDate >= $termDate)
{
return true;
} else {
return false;
}
}
Guys Please don't make it so complex The simple answer bellow
$date1=date('d_m_y');
$date2='31_12_11';
$date1=str_replace('_', '-', $date1);
$date2=str_replace('_', '-', $date2)
if(strtotime($date1) < strtotime($date2))
echo '1 is small ='.strtotime($date1).','.$date1;
else
echo '2 is small ='.strtotime($date2).','.$date2;
I just have added two more lines with your code
If both dates are in the same format then use a comparison operator.
$date1 = "2018-05-05";
$date2 = "2019-08-19";
//comparison operator to
if ($date1 > $date2) {
echo "$date1 is latest than $date2";
}
else{
echo "$date1 is older than $date2";
}
Output:
2018-05-05 is older than 2019-08-19
This question already has answers here:
How can I compare two dates in PHP?
(13 answers)
Closed 1 year ago.
How to compare two dates in php if dates are in format '03_01_12' and '31_12_11' .
I am using this code:
$date1=date('d_m_y');
$date2='31_12_11';
if(strtotime($date1) < strtotime($date2))
echo '1 is small ='.strtotime($date1).','.$date1;
else
echo '2 is small ='.strtotime($date2).','.$date2;
But its not working..
You will have to make sure that your dates are valid date objects.
Try this:
$date1=date('d/m/y');
$tempArr=explode('_', '31_12_11');
$date2 = date("d/m/y", mktime(0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]));
You can then perform the strtotime() method to get the difference.
Using DateTime::createFromFormat:
$format = "d_m_y";
$date1 = \DateTime::createFromFormat($format, "03_01_12");
$date2 = \DateTime::createFromFormat($format, "31_12_11");
var_dump($date1 > $date2);
Not answering the OPs actual problem, but answering just the title. Since this is the top result for "comparing dates in php".
Pretty simple to use Datetime Objects (php >= 5.3.0) and Compare them directly
$date1 = new DateTime("2009-10-11");
$date2 = new DateTime("tomorrow"); // Can use date/string just like strtotime.
var_dump($date1 < $date2);
The date_diff() function returns the difference between two DateTime objects.
If the first date is before the second date a positive number of days will be returned; otherwise a negative number of days:
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
output will be "+272 days" ;
changing
$date1 = "2014-03-15"
<?php
$date1=date_create("2014-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
Output will be "-93 days"
<?php
$expiry_date = "2017-12-31 00:00:00"
$today = date('d-m-Y',time());
$exp = date('d-m-Y',strtotime($expiry_date));
$expDate = date_create($exp);
$todayDate = date_create($today);
$diff = date_diff($todayDate, $expDate);
if($diff->format("%R%a")>0){
echo "active";
}else{
echo "inactive";
}
echo "Remaining Days ".$diff->format("%R%a days");
?>
Extending #nevermind's answer, one can use DateTime::createFromFormat: like,
// use - instead of _. replace _ by - if needed.
$format = "d-m-y";
$date1 = DateTime::createFromFormat($format, date('d-m-y'));
$date2 = DateTime::createFromFormat($format, str_replace("_", "-",$date2));
var_dump($date1 > $date2);
you can try something like:
$date1 = date_create('2014-1-23'); // format of yyyy-mm-dd
$date2 = date_create('2014-2-3'); // format of yyyy-mm-dd
$dateDiff = date_diff($date1, $date2);
var_dump($dateDiff);
You can then access the difference in days like this $dateDiff->d;
Try this
$data1 = strtotime(\date("d/m/Y"));
$data1 = date_create($data1);
$data2 = date_create("21/06/2017");
if($data1 < $data2){
return "The most current date is date1";
}
return "The most current date is date2";
You can to converte for integer number and compare.
Eg.:
$date_1 = date('Ymd');
$date_2 = '31_12_2011';
$date_2 = (int) implode(array_reverse(explode("_", $date_2)));
echo ($date_1 < $date_2) ? '$date_2 is bigger then $date_1' : '$date_2 is smaller than $date_1';
benchmark comparison
date_create, strtotime, DateTime, and direct
direct is faster
$f1="2014-12-12";
$f2="2014-12-13";
$diff=false;
$this->bench('a');
for ($i=0; $i < 20000; $i++) {
$date1=date_create($f1);
$date2=date_create($f2);
$diff=date_diff($date1,$date2);
}
$this->bench('a','b');
for ($i=0; $i < 20000; $i++) {
$date1=strtotime($f1);
$date2=strtotime($f2);
if ($date1>$date2) {
$diff=true;
}
}
$this->bench('b','c');
for ($i=0; $i < 20000; $i++) {
$date1 = new DateTime($f1);
$date2 = new DateTime($f2);
if ($date1>$date2) {
$diff=true;
}
}
$diff=false;
$this->bench('c','d');
for ($i=0; $i < 20000; $i++) {
if ($f1>$f2) {
$diff=true;
}
}
$this->bench('d','e');
var_dump($diff);
$this->dumpr($this->benchs);
results:
[a] => 1610415241.4687
[b] => 1610415242.8759
[a-b] => 1.407194852829
[c] => 1610415243.5672
[b-c] => 0.69137716293335
[d] => 1610415244.7036
[c-d] => 1.1363649368286
[e] => 1610415244.7109
[d-e] => 0.0073208808898926
compare the result of maketime() for each of the time
I know this is late, but for future reference, put the date format into a recognised format by using str_replace then your function will work. (replace the underscore with a dash)
//change the format to dashes instead of underscores, then get the timestamp
$date1 = strtotime(str_replace("_", "-",$date1));
$date2 = strtotime(str_replace("_", "-",$date2));
//compare the dates
if($date1 < $date2){
//convert the date back to underscore format if needed when printing it out.
echo '1 is small='.$date1.','.date('d_m_y',$date1);
}else{
echo '2 is small='.$date2.','.date('d_m_y',$date2);
}
Don't know what your problem is but:
function date_compare($d1, $d2)
{
$d1 = explode('_', $d1);
$d2 = explode('_', $d2);
$d1 = array_reverse($d1);
$d2 = array_reverse($d2);
if (strtotime(implode('-', $d1)) > strtotime(implode('-', $d2)))
{
return $d2;
}
else
{
return $d1;
}
}
I think this one is very simple function
function terminateOrNotStringtoDate($currentDate, $terminationdate)
{
$crtDate = new DateTime($currentDate);
$termDate = new DateTime($terminationdate);
if($crtDate >= $termDate)
{
return true;
} else {
return false;
}
}
Guys Please don't make it so complex The simple answer bellow
$date1=date('d_m_y');
$date2='31_12_11';
$date1=str_replace('_', '-', $date1);
$date2=str_replace('_', '-', $date2)
if(strtotime($date1) < strtotime($date2))
echo '1 is small ='.strtotime($date1).','.$date1;
else
echo '2 is small ='.strtotime($date2).','.$date2;
I just have added two more lines with your code
If both dates are in the same format then use a comparison operator.
$date1 = "2018-05-05";
$date2 = "2019-08-19";
//comparison operator to
if ($date1 > $date2) {
echo "$date1 is latest than $date2";
}
else{
echo "$date1 is older than $date2";
}
Output:
2018-05-05 is older than 2019-08-19
I created two datetime objects where $date1 = 09/02/2013 and $date2 = 03/02/2014
When I run the following code:
if ($date2 < $date1)
{
echo "hi";
}
for some reason it echos "hi" although $date2 is clearly greater than $date1. How am i supposed to compare these two dates? Please help!
<?php
$date1 = new DateTime ('2013-12-25');
$date2 = new DateTime ('2014-11-24');
if ($date1 > $date2) {
echo ('date1 is greater than date2');
}
else {
echo ('date2 is greater than date1');
}
use like below with function http://php.net/manual/en/function.strtotime.php
if (strtotime($date2 ) < strtotime($date1))
{
echo "hi";
}
hope this will sure help you.
That could work in JavaScript, but in PHP it will not :P
However, you could calculate an interval between dates.
$interval = $date1->diff($date2);
if ($interval->invert){ //1 if negative and 0 if positive
// $date2 has a bigger time value
} else {
// $date1 has a bigger time value
}