0

I want to check who is greater datetime between two datetime variables.

I have an string of datetime and i want to convert it into datetime format and check it who is greater?

my variables:

$d1 = '2016-02-02 07:35:00';
$d2 = '2016-02-1 13:10:31';

and I want to check who is greater between above two variables.

if($d1>$d2)
{
return true;
}
Manish Tiwari
  • 1,806
  • 10
  • 42
  • 65

7 Answers7

0

Use strtotime()

$d1 = strtotime('2016-02-02 07:35:00');
$d2 = strtotime('2016-02-1 13:10:31');

if($d1>$d2){
  return true;
} 
Daan
  • 12,099
  • 6
  • 34
  • 51
0

You can use DateTime

if (new DateTime($d1) > new DateTime($d2) {
   return true;
}
Petro Popelyshko
  • 1,353
  • 1
  • 15
  • 38
0

Try strtotime() function like this :

if(strtotime($d1) > strtotime($d2))
{
return true;
}
Thomas Rollet
  • 1,573
  • 4
  • 19
  • 33
0

There are comparison operators for the DateTime class in php . Something like this :

date_default_timezone_set('Europe/London');

$d1 = new DateTime('2008-08-03 14:52:10');
$d2 = new DateTime('2008-01-03 11:11:10');
var_dump($d1 == $d2);
var_dump($d1 > $d2);
var_dump($d1 < $d2);

Output

bool(false)
bool(true)
bool(false)
Drudge Rajen
  • 7,584
  • 4
  • 23
  • 43
0

try this

$d1 = strtotime('2016-02-02 07:35:00');
$d2 = strtotime('2016-02-1 13:10:31');

if($d1 > $d2)
{
  return true;
} 
progrAmmar
  • 2,606
  • 4
  • 29
  • 58
0

You can compare like that:

<?
$d1 = '2016-02-02 07:35:00';
$d2 = '2016-02-1 13:10:31';

$ts1 = strtotime($d1); // 1454394900
$ts2 = strtotime($d2); // 1454328631

if($ts1>$ts2) // 1454394900 > 1454328631 = true
{
    echo 1; // return this.
}
else{
    echo 0;
}

?>

Use strtotime() for both date and than compare.

devpro
  • 16,184
  • 3
  • 27
  • 38
0
<?php
$date = '1994-04-27 11:35:00';
$date1 = '2016-02-10 13:10:31';

$stt = strtotime($date); // 767439300

$stt1 = strtotime($date1); // 1455106231


if($stt>$stt1) 

    echo 1; 

else
    echo 0;


?>

use strtotime function to convert string to time formate...

RjD
  • 1
  • 1