Page 2 of 2

Posted: Mon Oct 16, 2006 11:49 am
by tommy1987
Okay so if I have the two dates,
e.g. $currentDate = 2006-10-16
$postDate = 2006-10-17
$currentTime = 12:12:12
$postTime = 12:12:12

I effectively want to compare them together to get the amount of seconds between them. Have looked around for a function on php.net and cannot find one, is there a friendly way of doign this?

Obviosuly the outcome of this would be 86400 seconds (1 exact day(24 hours)).

Thanks..

Posted: Mon Oct 16, 2006 11:52 am
by feyd
run the parts of each date-time through mktime(). Then subtract the results. You may need to perform an abs() to make sure the value is positive.

Posted: Mon Oct 16, 2006 2:22 pm
by tommy1987
Im using the following code with printf's function as mentioned in this thread.
Here is my code:

$secondsElapsed = $currentUNIXTS - $subUNIXTS;
$secondsElapsed = tick_count($secondsElapsed);
echo $secondsElapsed;

It just prints the word Array when i try this, how do i get it to print the string or access directly elements of the array which contain the number of days, months, hours etc...

Posted: Mon Oct 16, 2006 2:34 pm
by feyd
standard array access

Code: Select all

$array['elementName']
$array[3]

Posted: Mon Oct 16, 2006 3:06 pm
by tommy1987
The following code for working out the amount of time difference between now and a recent post does not work.
It gives a result but it is not correct, here is a screen shot of some output:

Date: 2006-10-16
Time: 16:57:21
Submitter: Test
Description:
Category: General
Array ( [years] => 0 [weeks] => 0 [days] => 0 [hours] => 04 [minutes] => 05 [seconds] => 02 )

This is obviously wrong since this was posted yesterday and I tried this just a moment ago!

Code: Select all

//mktime(hour, minute, second, month, day, year);
      
      //GET TODAYS DATETIME DETAILS...
      $currentDay     = date('d');
      $currentMinute  = date('i');
      $currentHour    = date('H');
      $currentSecond  = date('s');
      $currentYear    = date('Y');
      $currentMonth   = date('m');
      
      //CREATE ME A UNIX TIMESTAMP OF THIS POINT IN TIME...
      $currentUNIXTS = mktime($currentHour, 
                              $currentMinute, 
                              $currentSecond, 
                              $currentMonth, 
                              $currentDay, 
                              $currentYear);

      //GET DETAILS OF THE SUBMITTED TIME...
      $subDate = explode('-',$row['date']);
      $subTime = explode(':',$row['time']);
      
      $subDay         = $subDate[2];
      $subMinute      = $subTime[1];
      $subHour        = $subTime[0];
      $subSecond      = $subTime[2];
      $subYear        = $subDate[0];
      $subMonth       = $subDate[1];
      
      //CREATE ME A UNIX TIMESTAMP OF SUBMISSION TIME FOR COMPARING...
      $subUNIXTS      = mktime($subHour, 
                               $subMinute, 
                               $subSecond, 
                               $subMonth, 
                               $subDay, 
                               $subYear);      
      
      //OBTAIN TIME PASSED SINCE SUBMITTAL...
      $secondsElapsed = $currentUNIXTS - $subUNIXTS;
      $secondsArr = tick_count($secondsElapsed);
      //echo $secondsElapsed['minutes']; DEFINITELY WORKS
      abs($secondsArr);
      print_r($secondsArr);
This uses printf's function which is in the code snippets area of the forums..
Please help, Im at my wits end..!

Posted: Mon Oct 16, 2006 3:08 pm
by tommy1987
oh im sorry, i must be dumb, it is working! Ignore this please. Thanks for all the help?!

Posted: Mon Oct 16, 2006 3:13 pm
by feyd
tommy1987 wrote:

Code: Select all

//mktime(hour, minute, second, month, day, year);
      
      //GET TODAYS DATETIME DETAILS...
      $currentDay     = date('d');
      $currentMinute  = date('i');
      $currentHour    = date('H');
      $currentSecond  = date('s');
      $currentYear    = date('Y');
      $currentMonth   = date('m');
      
      //CREATE ME A UNIX TIMESTAMP OF THIS POINT IN TIME...
      $currentUNIXTS = mktime($currentHour, 
                              $currentMinute, 
                              $currentSecond, 
                              $currentMonth, 
                              $currentDay, 
                              $currentYear);
simplifies to

Code: Select all

$currentUNIXTS = time();
If the date you are checking against is coming from a database, the database may have functionality to convert its own date-time format to a unix-time form. For instance MySQL has UNIX_TIMESTAMP().

Posted: Mon Oct 16, 2006 3:15 pm
by tommy1987
Thank you very much feyd, you have been very helpful.