I have a Date object ( from Pear) and want to subtract another Date object to get the time difference in seconds.
我有一个Date对象(来自Pear)并且想要减去另一个Date对象以获得以秒为单位的时间差。
I have tried a few things but the first just gave me the difference in days, and the second would allow me to convert one fixed time to unix timestamp but not the Date object.
我尝试了一些东西,但第一个只是给了我几天的差异,第二个允许我将一个固定时间转换为unix时间戳而不是Date对象。
$now = new Date();
$tzone = new Date_TimeZone($timezone);
$now->convertTZ($tzone);
$start = strtotime($now);
$eob = strtotime("2009/07/02 17:00"); // Always today at 17:00
$timediff = $eob - $start;
** Note ** It will always be less than 24 hours difference.
**注意**它总是不到24小时的差异。
5 个解决方案
#1
Still gave somewhat wrong values but considering I have an old version of PEAR Date around, maybe it works for you or gives you an hint on how to fix :)
仍然给出了一些错误的值,但考虑到我有一个旧版本的PEAR日期,也许它适用于你或给你一个如何修复的提示:)
<pre>
<?php
require "Date.php";
$now = new Date();
$target = new Date("2009-07-02 15:00:00");
//Bring target to current timezone to compare. (From Hawaii to GMT)
$target->setTZByID("US/Hawaii");
$target->convertTZByID("America/Sao_Paulo");
$diff = new Date_Span($target,$now);
echo "Now (localtime): {$now->format("%Y-%m-%d %H:%M:%S")} \n\n";
echo "Target (localtime): {$target->format("%Y-%m-%d %H:%M:%S")} \n\n";
echo $diff->format("Diff: %g seconds => %C");
?>
</pre>
#2
Are you sure that the conversion of Pear Date object -> string -> timestamp will work reliably? That is what is being done here:
您确定Pear Date对象 - > string - > timestamp的转换是否可靠?这就是在这里做的:
$start = strtotime($now);
As an alternative you could get the timestamp like this according to the documentation
作为替代方案,您可以根据文档获得这样的时间戳
$start = $now->getTime();
#3
To do it without pear, to find the seconds 'till 17:00 you can do:
要做到没有梨,要找到直到17:00的秒,你可以这样做:
$current_time = mktime ();
$target_time = strtotime (date ('Y-m-d'. ' 17:00:00'));
$timediff = $target_time - $current_time;
Not tested it, but it should do what you need.
没有测试它,但它应该做你需要的。
#4
I don't think you should be passing the entire Date object to strtotime. Use one of these instead;
我不认为你应该将整个Date对象传递给strtotime。请改用其中一个;
$start = strtotime($now->getDate());
or
$start = $now->getTime();
#5
Maybe some folks wanna have the time difference the facebook way. It tells you "one minute ago", or "2 days ago", etc... Here is my code:
也许有些人想要facebook的时间差。它告诉你“一分钟前”,或“2天前”等...这是我的代码:
function getTimeDifferenceToNowString($timeToCompare) {
// get current time
$currentTime = new Date();
$currentTimeInSeconds = strtotime($currentTime);
$timeToCompareInSeconds = strtotime($timeToCompare);
// get delta between $time and $currentTime
$delta = $currentTimeInSeconds - $timeToCompareInSeconds;
// if delta is more than 7 days print the date
if ($delta > 60 * 60 * 24 *7 ) {
return $timeToCompare;
}
// if delta is more than 24 hours print in days
else if ($delta > 60 * 60 *24) {
$days = $delta / (60*60 *24);
return $days . " days ago";
}
// if delta is more than 60 minutes, print in hours
else if ($delta > 60 * 60){
$hours = $delta / (60*60);
return $hours . " hours ago";
}
// if delta is more than 60 seconds print in minutes
else if ($delta > 60) {
$minutes = $delta / 60;
return $minutes . " minutes ago";
}
// actually for now: if it is less or equal to 60 seconds, just say it is a minute
return "one minute ago";
}
#1
Still gave somewhat wrong values but considering I have an old version of PEAR Date around, maybe it works for you or gives you an hint on how to fix :)
仍然给出了一些错误的值,但考虑到我有一个旧版本的PEAR日期,也许它适用于你或给你一个如何修复的提示:)
<pre>
<?php
require "Date.php";
$now = new Date();
$target = new Date("2009-07-02 15:00:00");
//Bring target to current timezone to compare. (From Hawaii to GMT)
$target->setTZByID("US/Hawaii");
$target->convertTZByID("America/Sao_Paulo");
$diff = new Date_Span($target,$now);
echo "Now (localtime): {$now->format("%Y-%m-%d %H:%M:%S")} \n\n";
echo "Target (localtime): {$target->format("%Y-%m-%d %H:%M:%S")} \n\n";
echo $diff->format("Diff: %g seconds => %C");
?>
</pre>
#2
Are you sure that the conversion of Pear Date object -> string -> timestamp will work reliably? That is what is being done here:
您确定Pear Date对象 - > string - > timestamp的转换是否可靠?这就是在这里做的:
$start = strtotime($now);
As an alternative you could get the timestamp like this according to the documentation
作为替代方案,您可以根据文档获得这样的时间戳
$start = $now->getTime();
#3
To do it without pear, to find the seconds 'till 17:00 you can do:
要做到没有梨,要找到直到17:00的秒,你可以这样做:
$current_time = mktime ();
$target_time = strtotime (date ('Y-m-d'. ' 17:00:00'));
$timediff = $target_time - $current_time;
Not tested it, but it should do what you need.
没有测试它,但它应该做你需要的。
#4
I don't think you should be passing the entire Date object to strtotime. Use one of these instead;
我不认为你应该将整个Date对象传递给strtotime。请改用其中一个;
$start = strtotime($now->getDate());
or
$start = $now->getTime();
#5
Maybe some folks wanna have the time difference the facebook way. It tells you "one minute ago", or "2 days ago", etc... Here is my code:
也许有些人想要facebook的时间差。它告诉你“一分钟前”,或“2天前”等...这是我的代码:
function getTimeDifferenceToNowString($timeToCompare) {
// get current time
$currentTime = new Date();
$currentTimeInSeconds = strtotime($currentTime);
$timeToCompareInSeconds = strtotime($timeToCompare);
// get delta between $time and $currentTime
$delta = $currentTimeInSeconds - $timeToCompareInSeconds;
// if delta is more than 7 days print the date
if ($delta > 60 * 60 * 24 *7 ) {
return $timeToCompare;
}
// if delta is more than 24 hours print in days
else if ($delta > 60 * 60 *24) {
$days = $delta / (60*60 *24);
return $days . " days ago";
}
// if delta is more than 60 minutes, print in hours
else if ($delta > 60 * 60){
$hours = $delta / (60*60);
return $hours . " hours ago";
}
// if delta is more than 60 seconds print in minutes
else if ($delta > 60) {
$minutes = $delta / 60;
return $minutes . " minutes ago";
}
// actually for now: if it is less or equal to 60 seconds, just say it is a minute
return "one minute ago";
}