5
votes

I have a Date object ( from Pear) and want to subtract another Date object to get the time difference in seconds.

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.

        $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.

5
Is the output format of $now the same as the string you feed into strtotime()? ie "yyyy/mm/dd H:i"Mathew
Probably it's only me but I can't seem able to find description for a "Date" class in the PHP documentation. What library it's from?Milen A. Radev
Why do you convert only one of them to another Timezone? Shouldn't you either use both as local time or both on the target TZ?Carlos Lima
The purpose which I should have stated is to find out how much time do I have until 17:00 in another timezone, actuallys its a look so many other timezones.Brian G
Updated my answer according to your purpose, hope it helps.Carlos Lima

5 Answers

1
votes

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 :)

<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>
0
votes

Are you sure that the conversion of Pear Date object -> string -> timestamp will work reliably? That is what is being done here:

$start = strtotime($now);

As an alternative you could get the timestamp like this according to the documentation

$start = $now->getTime();
0
votes

To do it without pear, to find the seconds 'till 17:00 you can do:

$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.

0
votes

I don't think you should be passing the entire Date object to strtotime. Use one of these instead;

$start = strtotime($now->getDate());

or

$start = $now->getTime();
0
votes

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:

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

    }