php:datetime()在2个datetime与2个变量之间的差异 [英] php: datetime() difference between 2 datetime with 2 variables
问题描述
Date_in | Time_in | Date_out | Time_out
Date_in和Time_in属于一起(例如2013-02-18 13:00:00)和date_out与Time_out一起使用。我想知道我之间的区别,直到:
$ start_time = new DateTime('$ list [date_in]。$ list [time_in]');
$ since_start = $ start_time-> diff(new DateTime('$ list [date_out]。$ list [time_out]'));
$ hours = $ since_start-> h。'hours';
但它不工作。我认为我的报价和双引号都被弄乱了,因为使用'。真的让我感到困惑。
提前感谢关于我如何修复我的代码的建议!
感谢大家所有的详细帮助!我只是意识到服务器不支持PHP 5.3和我不能使用datetime()。
所以我的解决方案是:
code> $ start_time = strtotime($ list [date_in]。$ list [time_in]);
$ end_time = strtotime($ list [date_out]$ list [time_out] );
$ hours = abs(($ end_time - $ start_time)/ 3600);
尝试这个,
function datediff($ date1,$ date2)
{
$ diff = abs(strtotime($ date1) - strtotime($ date2));
return sprintf
(
%d Days, d小时,%d Mins,%d Seconds,
intval($ diff / 86400),
intval(($ diff%86400)/ 3600 ),
intval(($ diff / 60)%60),
intval($ diff%60)
);
}
print datediff(2013年2月18日,现在)。 \\\
;
OR
您可以使用 DateTime :: diff
$ start_date = new DateTime(2012-02-10 11:26:00);
$ end_date = new DateTime(2012-04-25 01:50:00);
$ interval = $ start_date-> diff($ end_date);
echoResult。 $ interval-> y。 年。 $ interval-> m。months,。$ interval-> d。days;
编辑:
结帐链接
可能会帮助您。
I have in my database 4 columns which are:
Date_in | Time_in | Date_out | Time_out
Date_in and Time_in belong together (e.g., 2013-02-18 13:00:00) and date_out goes with Time_out. I would like to find out the difference between and I have gotten up till:
$start_time = new DateTime("'$list[date_in] "."$list[time_in]'");
$since_start = $start_time->diff(new DateTime("'$list[date_out] "."$list[time_out]'"));
$hours = $since_start->h.' hours';
But it doesn't work. I think my quotes and double quotes are all messed up because the use of " ' . really confuses me..
Thanks in advance for any advice on how I can fix my code!
[EDIT] Thanks everyone for all your detailed help! I just realised that the server doesn't support php 5.3 and I can't use datetime().
So my solution was:
$start_time = strtotime("$list[date_in] " . "$list[time_in]");
$end_time = strtotime("$list[date_out] " . "$list[time_out]");
$hours = abs(($end_time - $start_time)/3600);
Try this,
function datediff( $date1, $date2 )
{
$diff = abs( strtotime( $date1 ) - strtotime( $date2 ) );
return sprintf
(
"%d Days, %d Hours, %d Mins, %d Seconds",
intval( $diff / 86400 ),
intval( ( $diff % 86400 ) / 3600),
intval( ( $diff / 60 ) % 60 ),
intval( $diff % 60 )
);
}
print datediff( "18th February 2013", "now" ) . "\n";
OR
You can use DateTime::diff
$start_date = new DateTime("2012-02-10 11:26:00");
$end_date = new DateTime("2012-04-25 01:50:00");
$interval = $start_date->diff($end_date);
echo "Result " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
EDIT:
Checkout links,
How to calculate the difference between two dates using PHP?
Php Date Time – 7 Methods to Calculate the Difference between 2 dates.
may help you.
这篇关于php:datetime()在2个datetime与2个变量之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!