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;
}
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)
Use strtotime()
$d1 = strtotime('2016-02-02 07:35:00');
$d2 = strtotime('2016-02-1 13:10:31');
if($d1>$d2){
return true;
}
You can use DateTime
if (new DateTime($d1) > new DateTime($d2) {
return true;
}
Try strtotime()
function like this :
if(strtotime($d1) > strtotime($d2))
{
return true;
}
try this
$d1 = strtotime('2016-02-02 07:35:00');
$d2 = strtotime('2016-02-1 13:10:31');
if($d1 > $d2)
{
return true;
}
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.
<?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...