0

I have a variable say $A='2015-05-12 10:00:00'.

Now I need to compare the above variable with the current date and time and find the time difference between those two.

Requirement is to enable a div tag only when the time difference is 3 hours or less.

P.S: $A is a variable. How can I convert it into Date and time and then compare with the current date and time.?

Currently tried Code is

$TodaysDate=date('Y-m-d');
$A=$Date.' '.$StartTime; 
$Todaysdate=date_create(date("Y-m-d ", strtotime($Todaysdate)));
$Final_Date=date_create(date("Y-m-d ", strtotime($A)));
$Diff_Date = date_diff($Todaysdate, $Final_Date);
$Total_Diff=$Diff_Date->format('%R%a');

Which will give Date difference.But exactly what i need is 3 Hours difference in time.Any help appreciated.

Razib
  • 10,965
  • 11
  • 53
  • 80
  • First parse date using (date_parse("2006-12-12 10:00:00.5")). Then comparise using if(new DateTime() > new DateTime($A)). – Pawan May 08 '15 at 06:27
  • possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Narendrasingh Sisodia May 08 '15 at 06:28

1 Answers1

0

I hope this will helps you..

$A='2015-05-12 10:00:00';
$Todaysdate = strtotime("now");
$Final_Date= strtotime($A);
$diff=$Todaysdate-$Final_Date;
echo ($diff/3600)." hours ";
noufalcep
  • 3,446
  • 15
  • 33
  • 51