将字符串转换为日期时间并与当前日期进行比较

时间:2023-01-21 20:21:02

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

我有一个变量说$ 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.

要求是仅在时差为3小时或更短时启用div标签。

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

P.S:$ A是一个变量。如何将其转换为日期和时间,然后与当前日期和时间进行比较。

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.

这将给出日期差异。但正是我需要的是3小时的时间差异。任何帮助表示赞赏。

1 个解决方案

#1


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 ";

#1


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 ";